我正在尝试将List<int>
从View
传递到我的Controller
。我尝试了多种方法,使用不同的参数。没有运气。
我目前正在尝试将{[4,5,13]}
传递给参数为List<int> ids
的方法。
发送数字列表或数组的正确格式是什么?
答案 0 :(得分:6)
我相信你必须传递这样的东西:
{"ids":[4,5,13]}
如果您正在使用AJAX,那么我记得我必须将traditional
设置为true (See the jquery ajax documentation)这样才能正确解析参数。
答案 1 :(得分:3)
AJAX方式:
来自AJAX的jquery文档:
数据
键入: PlainObject或String
要发送到服务器的数据。它是 转换为查询字符串,如果还不是字符串。它附加到 GET请求的URL。请参阅processData选项以防止这种情况 自动处理。对象必须是键/值对。如果价值是 Array,jQuery使用相同的密钥序列化多个值 传统环境的价值(如下所述)。传统
类型:布尔值
如果要使用,请将此项设置为true 传统风格的param serialization。
var roleList = new Array();
var usrId = "testuser";
/* one way of adding items to array */
roleList.push(1);
roleList.push(5);
roleList.push(8);
$.ajax({
type: "POST",
traditional: true, /* I M P O R T A N T */
url: /* url to controller goes here */,
data: { userId: usrId, allowedRoles: roleList },
success: function(returndata) {
alert("Done");
},
error: function(returndata) {
alert("Error:\n" + returndata.responseText);
}
});