我已经google了这个并检查了StackOverflow,但我必须遗漏一些东西......我有一个不显眼的jQuery,只需点击一下按钮就可以了。它会对复选框进行计数,并将每个选中的复选框值添加到数组中。当我在jQuery中使用警告框但是数组从未进入控制器端时,列表是正确的。代码流到控制器,但我打破了var resolutionViewModel = new ResolutionViewModel();并检查trans - 参数为null。我是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); }
});
}
});
};
控制器端
[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);
}
答案 0 :(得分:2)
尝试让你的控制器接受一个List,而不只是一个字符串(因为你没有传递一个字符串):
[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);
}
答案 1 :(得分:1)
发布JSON需要在控制器方法中为命名属性匹配参数。检查Chrome开发工具中的“网络”标签,看看您发布的内容,可能是这样的:
"{\"value\":\"...\"}"
没有value
属性传递给控制器方法的value
参数。我认为最好只是摆脱`JSON.stringify'并接受类似Colin的答案的列表,但是如果你想把它作为一个字符串,那么JSON字符串需要是一个对象的value属性,而不是反过来说:
data: {value : JSON.stringify(selectedTransactions)},
答案 2 :(得分:0)
尝试按如下方式传递数组:
data:"{'trans':" + JSON.stringify(selectedTransactions)+"}"
您的方法应如下:
public void Method(List<string> trans)
{
//Your implementation
}
答案 3 :(得分:0)
<强>解:强> $ .ajax回发未向控制器发送格式正确的数据。我通过使用IE中的网络选项卡并查看POSTed http的Request Body来发现这一点。它看起来像这样:transaction_table_length = 10&amp; chkTransaction = 22&amp; chkTransaction = 23 - 它应该看起来像这样:{“trans”:[“22”,“23”]}。为了解决这个问题,我将属性名称和数组进行了字符串化,如下所示,将dataType更改为“text”,并使控制器操作方法上的参数采用String [] trans。
的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: 'text',
url: 'http://localhost/AuditLog/Home/ResolutionFormDisplay',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ trans:selectedTransactions }),
traditional: true,
success: function (data) { alert(data); },
error: function(xhr, status, errorThrown) { alert(" Error: " + errorThrown); }
});
} else {
alert('You must select (check) at least one transaction to apply a resolution.');
return false;
}
return false;
});
};
MVC 4控制器动作
[HttpPost]
public PartialViewResult ResolutionFormDisplay(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);
}