通过jQuery ajax将JSON对象数组发布到MVC3操作方法

jcv*_*dan 11 ajax jquery json asp.net-mvc-3

模型绑定器是否不支持JSON对象的数组?下面的代码在发送单个JSON域对象作为ajax帖子的一部分时起作用.但是,在发送JSON域对象数组时,action参数为null.

     var domains = [{
                        DomainName: 'testt1',
                        Price: '19.99',
                        Available: true
                    }, {
                        DomainName: 'testt2',
                        Price: '15.99',
                        Available: false
                    }];

                $.ajax({
                    type: 'POST',
                    url: Url.BasketAddDomain,
                    dataType: "json",
                    data: domains,
                    success: function (basketHtml) {

                    },
                    error: function (a, b, c) {
                        alert('A problem ocurred');
                    }
            });
Run Code Online (Sandbox Code Playgroud)

这是动作方法:

public ActionResult AddDomain(IEnumerable<DomainBasketItemModel> domain)
{
    ...
Run Code Online (Sandbox Code Playgroud)

任何想法,如果有可能这样做?

编辑

@Milimetric

您的解决方案有效 但是,这是我的错,但我演示的代码不是我的问题的真实代码,我试图显示更容易理解的等效代码.

我实际上正在创建一个数组,然后交换一些DOM元素并将JSON对象推送到数组上,然后将此数组作为数据发布...

var domains = [];

                $(this).parents('table').find('input:checked').each(function () {
                    var domain = {
                        DomainName: $(this).parent().parent().find('.name').html(),
                        Price: $(this).parent().parent().find('.price span').html(),
                        Available: $(this).parent().parent().find('.available').html() == "Available"
                    }

                    domains.push(domain);
                });

                $.ajax({
                    type: 'POST',
                    url: Url.BasketAddDomain,
                    dataType: "json",
                    data: { domain: domains },
                    success: function (basketHtml) {

                    },
                    error: function (a, b, c) {
                        alert('A problem ocurred');
                    }
                });
Run Code Online (Sandbox Code Playgroud)

Mil*_*ric 32

你需要:

var domains = { domains: [... your elements ...]};

            $.ajax({
                type: 'post',
                url: 'Your-URI',
                data: JSON.stringify(domains),
                contentType: "application/json; charset=utf-8",
                traditional: true,
                success: function (data) {
                    ...
                }
            });
Run Code Online (Sandbox Code Playgroud)

通常,检查调试器中的Request对象,您将看到正在传递的内容,并了解如何"说"HTTP.

注意:刚刚发现了这一点.模型绑定器会阻塞可为空的十进制属性,例如:

public decimal? latitude { get; set; }
Run Code Online (Sandbox Code Playgroud)

因此,如果您使用看起来像这样的json字符串发布它,它将不会绑定它:

{"latitude":12.0}
Run Code Online (Sandbox Code Playgroud)

但如果你发布这样的东西它会工作:

{"latitude":"12.0"}
Run Code Online (Sandbox Code Playgroud)

请参阅:http://syper-blogger.blogspot.com/2011/07/hello-world.html

  • 感谢您的回复 - 以及正确的解决方案,但我已经更新了我的问题 (2认同)
  • K,编辑我的答案,我认为关键是JSON.stringify和contentType:"application/json ..." (2认同)
  • +1 Nice解决方案(虽然你不需要`traditional:true`,因为你的数据只是``JSON.stringify(...)`之后的`string`而``traditional`选项只是为了它如何序列化复杂对象). (2认同)