【发布时间】:2018-07-04 09:35:15
【问题描述】:
我知道这个问题在 SO 中有一些已经回答的替代方案,但这些解决方案都没有对我有用。
我有一个整数数组,我想通过AJAX 将POST 传递给一个方法。但不知何故,控制器中的参数总是null。
这是我的JS:
function addSelected(cID, pID) {
var id, idList = [];
$("#FilteredCategory_" + pID + "_" + cID + " .content ul li a").each(function () {
id = parseInt(this.className.replace(/[^0-9]/gi, ''), 10);
idList.push(id);
});
$.ajax('@Url.Action("SelectedAdd","Home")', {
type: "POST",
traditional: true,
data: idList,
success: function (data) {
alert(data);
}
});
}
POST 控制器中的方法:
[HttpPost]
public string SelectedAdd(ICollection<int> topicIds)
{
string res = string.Empty;
foreach (var id in topicIds)
{
res += $"Item in list: '{id}' \n ";
}
return res;
}
任何建议,为什么topicIds 总是nul?
【问题讨论】:
-
请访问help center,使用tour查看内容和How to Ask。做一些研究,搜索关于 SO 的相关主题;如果您遇到困难,请发布您的尝试minimal reproducible example,并注明输入和预期输出。
-
显示相关的 HTML - 点击
<>sn-p 编辑器,给我们一个真实的例子。任何控制台错误?控制台记录 idList -
只是因为你在ajax请求中的参数和
c#的方法不一样。从data: idList更改为data: {topicIds: idList }。
标签: jquery ajax asp.net-mvc-4