Does the model binder not suport arrays of JSON objects? The code below works when sending a single JSON domain object as part of the ajax post. However, when sending an array of JSON domain objects, the action parameter is 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');
}
});

This is the action method:

public ActionResult AddDomain(IEnumerable<DomainBasketItemModel> domain)
{
...

Any ideas if it is possible to do this?

Answers

You need:

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

In general, check out the Request object in the debugger, you'll see what's being passed and get an idea of how to "speak" HTTP.

NOTE: Just found this out. The model binder chokes on nullable decimal properties like:

public decimal? latitude { get; set; }

So it won't bind that if you're posting to it with a json string that looks like this:

{"latitude":12.0}

But it WILL work if you post something like this:

{"latitude":"12.0"}

See: http://syper-blogger.blogspot.com/2011/07/hello-world.html

source:http://stackoverflow.com/questions/6031206/posting-array-of-json-objects-to-mvc3-action-method-via-jquery-ajax

Posting array of JSON objects to MVC3 action method via jQuery ajax的更多相关文章

  1. MVC3学习:利用jquery+ajax生成二维码(支持中文)

    二维码越来越热火了,做电子商务网站,不做二维码,你就OUT了. 一.下载DLL文件(ThoughtWorks.QRCode.dll),并在项目中引用.点此下载 如果你还不知道什么是QRCode二维码, ...

  2. How to receive JSON as an MVC 5 action method parameter

    How to receive JSON as an MVC 5 action method parameter  解答1 Unfortunately, Dictionary has problems ...

  3. Jquery Ajax方法传递json到action

    ajax向后台传入json需要设置option,如下 contentType:'application/json' data:Json.Stringify(jsObj) 后台处理复杂json对象(不知 ...

  4. python np array转json

    np array转json import numpy as np import codecs, json a = np.arange().reshape(,) # a by array b = a.t ...

  5. 再谈Jquery Ajax方法传递到action 【转载】

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://cnn237111.blog.51cto.com/2359144/984466 之 ...

  6. 再谈Jquery Ajax方法传递到action

     原始出处 :http://cnn237111.blog.51cto.com/2359144/984466  本人只是转载 原文如下: 假设 controller中的方法是如下: public Act ...

  7. 再谈Jquery Ajax方法传递到action(转)

    之前写过一篇文章Jquery Ajax方法传值到action,本文是对该文的补充. 假设 controller中的方法是如下: public ActionResult ReadPerson(Perso ...

  8. MVC中使用Ajax提交数据 Jquery Ajax方法传值到action

    Jquery Ajax方法传值到action <script type="text/javascript"> $(document).ready(function(){ ...

  9. php+jquery+ajax+json的一个最简单实例

    html页面: <html> <head> <meta http-equiv="content-type" content="text/ht ...

随机推荐

  1. 前端基础之JavaScript_2

    摘要: window对象 BOM(Browser Object Model) DOM (Document Object Model) 0.引子: JavaScript分为三部分:ECMAScript. ...

  2. 《算法导论》— Chapter 9 中位数和顺序统计学

    序 在算法导论的第二部分主要探讨了排序和顺序统计学,第六章~第八章讨论了堆排序.快速排序以及三种线性排序算法.该部分的最后一个章节,将讨论顺序统计方面的知识. 在一个由n个元素组成的集合中,第i个顺序 ...

  3. 【URAL 1486】Equal Squares(二维哈希+二分)

    Description During a discussion of problems at the Petrozavodsk Training Camp, Vova and Sasha argued ...

  4. 【BZOJ 2761】 不重复数字 (哈希算法)

    链接 http://www.lydsy.com/JudgeOnline/problem.php?id=2761 Description 给出N个数,要求把其中重复的去掉,只保留第一次出现的数. 例如, ...

  5. Haproxy配置文件详解

    #/usr/local/sbin/haproxy -f /etc/haproxy/haproxy.cfg -st `cat /var/run/haproxy.pid` ################ ...

  6. 大数据学习——Linux-SSH报错:Could not resolve hostname centos02: Temporary failure in name resolution

    https://blog.csdn.net/mcb520wf/article/details/83303792 随笔异常 ssh: Could not resolve hostname centos0 ...

  7. jquery取当前节点的上级ID

  8. SQL中varchar和nvarchar的基本介绍及其区别

    SQL中varchar和nvarchar的基本介绍及其区别 varchar(n) 长度为 n 个字节的可变长度且非 Unicode 的字符数据.n 必须是一个介于 1 和 8,000 之间的数值.存储 ...

  9. oc温习三:常用函数

    参考文章: 算术函数 [算术函数] 函数名 说明 int rand() 随机数生成.(例)srand(time(nil)); //随机数初期化int val = rand()P; //0-49之间的随 ...

  10. httpclient自动执行http的302重定向

    今天debug过程中发现,httpclient会自动执行302的重定向,但是这个的前提是第一个请求是get发出的.我测试发现用post的后的302是系统不会自动redirect的..不知道到底正确不, ...