我已经用谷歌搜索了这个并在 StackOverflow 上进行了检查,但我一定错过了一些东西......我有不引人注目的 jQuery,它劫持了一个简单的按钮点击。它对复选框进行计数,并将每个复选框的值添加到一个数组中。当我在 jQuery 中使用警报框时,列表是正确的,但数组永远不会到达 Controller 端。代码流向 Controller ,但我在 var resolutionViewModel=new ResolutionViewModel(); 处中断并检查 trans - 参数为空。我是 jQuery 新手,确实可以使用这里的帮助。
jQuery
// Return the selected transactions
function resolveTransactions() {
$('#btnResolve').click(function() {
var selectedTransactions = new Array();
$('input[name="chkTransaction"]:checked').each(function() {
selectedTransactions.push(this.value);
});
if (selectedTransactions.length > 0) {
$.ajax({
type: 'POST',
dataType: 'json',
url: 'http://localhost/AuditLog/Home/ResolutionFormDisplay',
contentType: 'application/json; charset=utf-8',
data: {trans : selectedTransactions},
traditional: true,
success: function (data) { alert(data); },
error: function(xhr, status, errorThrown) { alert("Error: " + errorThrown); }
});
}
});
};
Controller 端
[HttpPost]
public PartialViewResult ResolutionFormDisplay(List<string> trans)
{
var resolutionViewModel = new ResolutionViewModel();
// fill Usernames dropdown selector in ViewModel
// fill Status dropdown selector in ViewModel
// fill list of transactionIds in ViewModel
return PartialView("_ResolutionDialog", resolutionViewModel);
}
最佳答案
尝试让您的 Controller 接受一个列表,而不仅仅是一个字符串(因为您没有传递单个字符串):
[HttpPost]
public PartialViewResult ResolutionFormDisplay(List<string> value)
{
var resolutionViewModel = new ResolutionViewModel();
// fill Usernames dropdown selector in ViewModel
// fill Status dropdown selector in ViewModel
// fill list of transactionIds in ViewModel
return PartialView("_ResolutionDialog", resolutionViewModel);
}
关于javascript - 无法将数组从 jQuery 发送到 MVC 4 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21891372/