从Javascript MVC控制器传递数据
我对jQuery和图表真的很陌生。这是我的剧本,很好用。它为我提供了用户选择的复选框的id。我的控制器中有一个图表操作,它也可以正常工作,但它会使用我的所有值创建一个图表。我希望它根据脚本中的选定值创建图表。我不知道如何将所选值传递给我的控制器从Javascript MVC控制器传递数据,javascript,jquery,asp.net-mvc,highcharts,Javascript,Jquery,Asp.net Mvc,Highcharts,我对jQuery和图表真的很陌生。这是我的剧本,很好用。它为我提供了用户选择的复选框的id。我的控制器中有一个图表操作,它也可以正常工作,但它会使用我的所有值创建一个图表。我希望它根据脚本中的选定值创建图表。我不知道如何将所选值传递给我的控制器 var checkboxes = $("input[type='checkbox']"); $(function () { function getValueUsingClass() { /* declare an checkbo
var checkboxes = $("input[type='checkbox']");
$(function ()
{
function getValueUsingClass() {
/* declare an checkbox array */
var chkArray = [];
/* look for all checkboes that have a class 'chk' attached to it and check if it was checked */
$(".chk:checked").each(function () {
chkArray.push($(this).val());
});
/* we join the array separated by the comma */
var selected = chkArray.join(",") + ",";
/* check if there is selected checkboxes, by default the length is 1 as it contains one single comma */
if (selected.length > 1)
{
alert("You have selected " + selected);
}
else
{
alert("Please check at least one of the checkbox");
}
}
$("#Charter").click(function () {
getValueUsingClass();
});
});
使用
returnselected填充变量后,返回js函数中所需的数据
然后通过发布表单或使用ajax将其发回
将数据绑定到视图页面上的元素,例如:
<input name="foo" id="yourId" value="bar" />
并通过将表单发布到控制器来传递模型
var checkboxes = $("input[type='checkbox']");
$(function ()
{
function getValueUsingClass() {
/* declare an checkbox array */
var chkArray = [];
/* look for all checkboes that have a class 'chk' attached to it and check if it was checked */
$(".chk:checked").each(function () {
chkArray.push($(this).val());
});
/* we join the array separated by the comma */
var selected = chkArray.join(",") + ",";
/* check if there is selected checkboxes, by default the length is 1 as it contains one single comma */
if (selected.length > 1)
{
alert("You have selected " + selected);
}
else
{
alert("Please check at least one of the checkbox");
}
}
$("#Charter").click(function () {
getValueUsingClass();
});
});
如果希望将数据异步发送到控制器,则可以查看。在使用
返回所选变量填充变量后,在js函数中返回所需的数据
然后通过发布表单或使用ajax将其发回
将数据绑定到视图页面上的元素,例如:
<input name="foo" id="yourId" value="bar" />
并通过将表单发布到控制器来传递模型
var checkboxes = $("input[type='checkbox']");
$(function ()
{
function getValueUsingClass() {
/* declare an checkbox array */
var chkArray = [];
/* look for all checkboes that have a class 'chk' attached to it and check if it was checked */
$(".chk:checked").each(function () {
chkArray.push($(this).val());
});
/* we join the array separated by the comma */
var selected = chkArray.join(",") + ",";
/* check if there is selected checkboxes, by default the length is 1 as it contains one single comma */
if (selected.length > 1)
{
alert("You have selected " + selected);
}
else
{
alert("Please check at least one of the checkbox");
}
}
$("#Charter").click(function () {
getValueUsingClass();
});
});
如果您希望异步向控制器发送数据,则可以查看。您可以使用ajax在
getValueUsingClass()中调用控制器方法
它可能看起来像这样:
$.ajax({
url: "/YourControllerName/Chart",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: { arr: chkArray },
success: function () {
// do things upon success
},
error: function () {
alert("Error!");
}
});
也就是说,如果控制器操作有一个名为arr
的参数,因为json将chkArray
传递给控制器后映射到它。您可以使用ajax在getValueUsingClass()
中调用控制器方法
var checkboxes = $("input[type='checkbox']");
$(function ()
{
function getValueUsingClass() {
/* declare an checkbox array */
var chkArray = [];
/* look for all checkboes that have a class 'chk' attached to it and check if it was checked */
$(".chk:checked").each(function () {
chkArray.push($(this).val());
});
/* we join the array separated by the comma */
var selected = chkArray.join(",") + ",";
/* check if there is selected checkboxes, by default the length is 1 as it contains one single comma */
if (selected.length > 1)
{
alert("You have selected " + selected);
}
else
{
alert("Please check at least one of the checkbox");
}
}
$("#Charter").click(function () {
getValueUsingClass();
});
});
它可能看起来像这样:
$.ajax({
url: "/YourControllerName/Chart",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: { arr: chkArray },
success: function () {
// do things upon success
},
error: function () {
alert("Error!");
}
});
也就是说,如果控制器操作有一个名为arr
的参数,因为json将chkArray
传递给控制器后映射到它。您的操作方法不接受任何参数。您试图发送什么数据?我正在尝试将arrar chkArray中的值发送到我的控制器中的特许操作。在迭代等过程中,simpler is extract selected checkbox by:$('input[type=“checkbox”]:checked')。对于通信,您可以使用ajax并在控制器中返回JSON。然后在highcharts创建中使用$.getJSON()并加载数据/选项对不起,我对这一点不太熟悉,我有点理解您的意思,但我不确定如何进行。您的操作方法没有使用任何参数。您试图发送什么数据?我正在尝试将arrar chkArray中的值发送到我的控制器中的特许操作。在迭代等过程中,simpler is extract selected checkbox by:$('input[type=“checkbox”]:checked')。对于通信,您可以使用ajax并在控制器中返回JSON。然后在highcharts创建中使用$.getJSON()并加载数据/选项对不起,我对这一点不熟悉,我有点理解你的意思,但我不确定如何进行。
var checkboxes = $("input[type='checkbox']");
$(function ()
{
function getValueUsingClass() {
/* declare an checkbox array */
var chkArray = [];
/* look for all checkboes that have a class 'chk' attached to it and check if it was checked */
$(".chk:checked").each(function () {
chkArray.push($(this).val());
});
/* we join the array separated by the comma */
var selected = chkArray.join(",") + ",";
/* check if there is selected checkboxes, by default the length is 1 as it contains one single comma */
if (selected.length > 1)
{
alert("You have selected " + selected);
}
else
{
alert("Please check at least one of the checkbox");
}
}
$("#Charter").click(function () {
getValueUsingClass();
});
});