Eri*_*k W 5 javascript asp.net-mvc-3
我很难通过jQuery将JavaScript对象发布到.net MVC 3控制器.
我的目标:
var postData = {
'thing1' : "whatever",
'thing2' : "something else",
'thing3' : [1, 2, 3, 4]
}
Run Code Online (Sandbox Code Playgroud)
我的jQuery调用:
$.post('<%= Url.Action("Commit", "MassEdit") %>', postData, function (data) {
// stuff
});
Run Code Online (Sandbox Code Playgroud)
我的视图模型:
public class SubmitThing {
public string thing1 { get; set; }
public string thing2 { get; set; }
public IEnumerable<int> thing3 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的控制器:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Commit(SubmitThing changes)
{
return new EmptyResult();
}
Run Code Online (Sandbox Code Playgroud)
问题是我在控制器中拥有的'更改'对象的东西等于"无论什么",东西2等于"别的东西",但是thing3为空.现在我将thing3作为我的整数列表吗?
补充:我认为这更像是一个映射问题,而不是序列化问题.在我的控制器中,如果我看一下
HttpContext.Request.Form["thing3[]"]
Run Code Online (Sandbox Code Playgroud)
我得到一个值为"1,2,3,4"的字符串.但同样,我希望映射能够正常工作.
谢谢!
只需将其发布为json:
$.ajax({
url: '<%= Url.Action("Commit", "MassEdit") %>',
type: 'POST',
dataType: 'json',
data: JSON.stringify({'thing1' : "whatever",
'thing2' : "something else",
'thing3' : [1, 2, 3, 4]
}),
contentType: 'application/json; charset=utf-8',
success: function (data) {
}
});
Run Code Online (Sandbox Code Playgroud)
它应该工作