发布对象与jQuery的整数的列表.NET MVC 3控制器 [英] Posting object with list of ints from jquery to .net mvc 3 controller

查看:112
本文介绍了发布对象与jQuery的整数的列表.NET MVC 3控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有困难,通过jQuery发布一个JavaScript对象到.NET MVC 3控制器。

I'm having difficulty posting a JavaScript object via jQuery to a .net MVC 3 controller.

我的目标:

var postData = {
    'thing1' : "whatever",
    'thing2' : "something else",
    'thing3' : [1, 2, 3, 4]
}

我的jQuery的电话:

My jQuery Call:

$.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();
}

问题是,我的'变化'对象,我有我的控制器中有thing1等于无所谓,thing2等于别的东西,但thing3为空。现在做我得到thing3是我的整数列表?

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[]"]

我得到的1,2,3,4的值的字符串。但同样,我想的映射只是工作。

I get a string with the value of "1,2,3,4". But again, I'd like the mapping to just work.

谢谢!

推荐答案

刚张贴的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) {

        }
    });

和它应该工作

这篇关于发布对象与jQuery的整数的列表.NET MVC 3控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆