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. 【HIHOCODER 1478】 水陆距离(BFS)

    描述 给定一个N x M的01矩阵,其中1表示陆地,0表示水域.对于每一个位置,求出它距离最近的水域的距离是多少. 矩阵中每个位置与它上下左右相邻的格子距离为1. 输入 第一行包含两个整数,N和M. ...

  2. 【HIHOCODER1527 】 快速乘法

    描述 在写代码时,我们经常要用到类似 x × a 这样的语句( a 是常数).众所周知,计算机进行乘法运算是非常慢的,所以我们需要用一些加法.减法和左移的组合来实现乘一个常数这个操作.具体来讲, 我们 ...

  3. C语言 NULL 是什么鬼

    NULL , 0 , '\0'  之间的区别与联系 1.NULL 结构体的使用中,都可以用NULL表示空,那么NULL是什么 #ifndef __cplusplus #define NULL ((vo ...

  4. 关于 HTTP meta 的 IE=edge 说明

    http://www.oschina.net/question/54100_17414 陌生标记标记一: < meta http-equiv = "X-UA-Compatible&qu ...

  5. 大数据学习——linux常用命令(一)

    一.基本日常操作命令 1 查看当前所在工作目录的全路径 pwd 2 查看当前系统的时间 date 设置时间,date -s"2018-11-12" 修改时间后,需要写入硬件bios ...

  6. PTA 05-树8 File Transfer (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/670 5-8 File Transfer   (25分) We have a netwo ...

  7. 【组合 数学】codeforces C. Do you want a date?

    codeforces.com/contest/810/problem/C [题意] 给定一个集合A,求 , 输入: [思路] 基数为n的集合有2^n-1个非空子集. 首先n个数要从小到大排序,枚举最后 ...

  8. UltraEdit-14.10.0.1024版本语法着色配置

    用了UltraEdit有段时间了,一直没做语法着色,当做普通文本编辑器使用,这也太委屈这个“神器”了. 今天就让它物尽其用吧.体验一把UltraEdit的语法高亮功能. 参考:http://www.1 ...

  9. linux下部署一个JavaEE项目的简单步骤

    部署项目的前提是准备好环境,包括:Java运行环境(JDK),Tomcat,Mysql数据库 1.首先将东西上传到服务器,我是在windows安装ssh工具(下载安装git即可http://gitfo ...

  10. 简述WEB项目前端脚本的一次重构历程,labJs,requireJs实践[转载]

    重构前的状态:    大量的js代码混在繁多的Jsp文件中,对第三方的js库依赖也很杂乱.虽然在部分交互性较强的页面中,将js代码分离到了独立的js文件中,但是代码结构及依赖管理依然很乱.不说新人来了 ...