admin 管理员组文章数量: 1086019
I'm having difficulty posting a JavaScript object via jQuery to a MVC 3 controller.
My object:
var postData = {
'thing1' : "whatever",
'thing2' : "something else",
'thing3' : [1, 2, 3, 4]
}
My jQuery Call:
$.post('<%= Url.Action("Commit", "MassEdit") %>', postData, function (data) {
// stuff
});
My View Model:
public class SubmitThing {
public string thing1 { get; set; }
public string thing2 { get; set; }
public IEnumerable<int> thing3 { get; set; }
}
My controller:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Commit(SubmitThing changes)
{
return new EmptyResult();
}
The problem is that my 'changes' object that I have within my controller have thing1 equal to "whatever", thing2 equal to "something else", but thing3 is null. Now do I get thing3 to be my list of integers?
Added: I think this is more of a mapping issue than a serialization issue. In my controller, if I look at
HttpContext.Request.Form["thing3[]"]
I get a string with the value of "1,2,3,4". But again, I'd like the mapping to just work.
Thanks!
I'm having difficulty posting a JavaScript object via jQuery to a MVC 3 controller.
My object:
var postData = {
'thing1' : "whatever",
'thing2' : "something else",
'thing3' : [1, 2, 3, 4]
}
My jQuery Call:
$.post('<%= Url.Action("Commit", "MassEdit") %>', postData, function (data) {
// stuff
});
My View Model:
public class SubmitThing {
public string thing1 { get; set; }
public string thing2 { get; set; }
public IEnumerable<int> thing3 { get; set; }
}
My controller:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Commit(SubmitThing changes)
{
return new EmptyResult();
}
The problem is that my 'changes' object that I have within my controller have thing1 equal to "whatever", thing2 equal to "something else", but thing3 is null. Now do I get thing3 to be my list of integers?
Added: I think this is more of a mapping issue than a serialization issue. In my controller, if I look at
HttpContext.Request.Form["thing3[]"]
I get a string with the value of "1,2,3,4". But again, I'd like the mapping to just work.
Thanks!
Share Improve this question edited Jun 15, 2011 at 16:49 Erik W asked Jun 15, 2011 at 16:31 Erik WErik W 8079 silver badges26 bronze badges3 Answers
Reset to default 6Just post it as 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) {
}
});
and it should work
Hi why don't you join the values before passing them?
var thing3 = [1, 2, 3, 4];
thing3 = thing3.join(',');
var postData = {
'thing1' : "whatever",
'thing2' : "something else",
'thing3' : thing3
}
otherwise i think you have to use the $.ajax function to have it serialaize the array
I'm doing something similar in a web forms project, but I'm using List<int>
instead of IEnumberable<int>
(edit to add:) And it's working... ;)
EDIT 2 :
Looking farther at what we're doing different. Could you try building your object slightly differently and using JSON.stringify()
? The following is a bit closer to what's working for me...
var postData = JSON.stringify({
thing1 : "whatever",
thing2 : "something else",
thing3 : [1, 2, 3, 4]
});
本文标签: javascriptPosting object with list of ints from jquery to net mvc 3 controllerStack Overflow
版权声明:本文标题:javascript - Posting object with list of ints from jquery to .net mvc 3 controller - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744007319a2517519.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论