我很难通过jQuery将jQuery对象发布到.net MVC 3控制器。
我的目标:
var postData = {
'thing1' : "whatever",
'thing2' : "something else",
'thing3' : [1, 2, 3, 4]
}我的jQuery电话:
$.post('<%= Url.Action("Commit", "MassEdit") %>', postData, function (data) {
// stuff
});我的视图模型:
public class SubmitThing {
public string thing1 { get; set; }
public string thing2 { get; set; }
public IEnumerable<int> thing3 { get; set; }
}我的控制器:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Commit(SubmitThing changes)
{
return new EmptyResult();
}问题是,我在控制器中拥有的'changes‘对象的thing1等于“任何”,thing2等于“其他东西”,但是thing3是空的。现在,我是否让thing3作为我的整数列表?
补充道:,我认为这与其说是序列化问题,不如说是映射问题。在我的控制器里,如果我看
HttpContext.Request.Form["thing3[]"]我得到一个值为"1,2,3,4“的字符串。不过,我还是希望这张地图能正常工作。
谢谢!
发布于 2011-06-15 17:00:32
把它写成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) {
}
});它应该能起作用
发布于 2011-06-15 16:39:03
嗨,为什么不在传递值之前加入这些值呢?
var thing3 = [1, 2, 3, 4];
thing3 = thing3.join(',');
var postData = {
'thing1' : "whatever",
'thing2' : "something else",
'thing3' : thing3
}否则,我认为您必须使用$.ajax函数来序列化数组。
发布于 2011-06-15 16:40:38
我在一个web表单项目中做类似的事情,但是我使用List<int>而不是IEnumberable<int> (编辑以添加:),它正在工作.;)
编辑2:
再看一看我们在做什么。您能尝试使用JSON.stringify()稍微不同地构建对象吗?下面的内容更接近于我的工作.
var postData = JSON.stringify({
thing1 : "whatever",
thing2 : "something else",
thing3 : [1, 2, 3, 4]
});https://stackoverflow.com/questions/6361078
复制相似问题