[英]Passing data from Javascript MVC controller
我真的是jQuery和Charts的新手。 這是我的腳本,工作正常。 它給了我用戶選擇的復選框的ID。 我的控制器中有一個Chart動作,它也可以正常工作,但是它使用我的所有值創建了一個圖表。 我希望它根據腳本中所選的值創建一個圖表。 我不知道如何將選定的值傳遞給控制器。
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();
});
});
使用return selected;
select填充變量后,在js函數中返回所需的數據return selected;
然后通過發布表單或使用Ajax將其發送回去。
將數據綁定到“視圖”頁面上的元素,例如:
<input name="foo" id="yourId" value="bar" />
然后修改它的值:
$('#foo').val(getValueUsingClass());
並通過將表單發布到控制器來傳遞模型。
如果希望將數據異步發送到控制器,則可以研究Ajax 。
您可以使用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!");
}
});
也就是說,提供您的Controller操作具有一個名為arr
的參數,因為一旦將chkArray
傳遞給Controller,json chkArray
將其映射到它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.