如果ajax发布一个数组,我需要在我的操作中使用什么对象类型?
我尝试了以下操作,操作列表始终为空:如果ajax发布一个数组,我需要在我的操作中使用什么对象类型?,ajax,json,asp.net-mvc-3,Ajax,Json,Asp.net Mvc 3,我尝试了以下操作,操作列表始终为空: $('td').delegate('.roleoption select', 'change', function() { var userRoles = new Array(); $(this).parent().parent().find('select').each(function() { userRoles.push($(this).val()); }); console.log(userRoles
$('td').delegate('.roleoption select', 'change', function() {
var userRoles = new Array();
$(this).parent().parent().find('select').each(function() {
userRoles.push($(this).val());
});
console.log(userRoles); // [1, 4, 3]
var userId = $(this).parent().parent().parent().find('td').first().text();
console.log(userId); // [2]
$.ajax({
url: '@Url.Action("CambiarRol", "Administracion")',
type: 'POST',
data: { user: userId, roles: userRoles },
success: function(result) {
alert("Worked ok.");
}
});
});
我的行动是:
public ActionResult CambiarRol(int user, List<int> roles)
{
// "user" is received correctly. "roles" is null.
return Json(new object());
}
在呼叫post之前设置此选项 jQuery.ajaxSettings.traditional=true 我发现这是因为我意识到你发送的数组实际上是用key=roles[]发送的,这就是为什么你总是在控制器端得到null
如果您更改上述内容,则关键将是角色,您将获得INT列表。在调用post之前设置此项 jQuery.ajaxSettings.traditional=true 我发现这是因为我意识到你发送的数组实际上是用key=roles[]发送的,这就是为什么你总是在控制器端得到null
如果您更改上述内容,则关键将是角色,您将获得Int列表。可能重复?-尝试使用$.toJSONarray。这里有更多解释:@djserva:它不起作用,也不能解决问题。还有其他想法吗?联系但不能解决我的问题:如果在JavaScript中将角色转换为JSON,那么动作中角色的类型需要是字符串。如果您需要在操作中将其返回到对象中,那么您将使用JavascriptSerializer。是否可能重复?-尝试使用$.toJSONarray。这里有更多解释:@djserva:它不起作用,也不能解决问题。还有其他想法吗?联系但不能解决我的问题:如果在JavaScript中将角色转换为JSON,那么动作中角色的类型需要是字符串。如果您需要在操作中将其返回到对象中,那么您将使用JavascriptSerializer。
success: function(result) {
alert("Worked ok.");
}