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. Django框架基础知识06-模型基础

    1.数据库的连接配置 django 连接mysql的配置流程: 安装 pymysql pip install pymysql 创建数据库用户 有创建数据库权限的用户 创建数据库 crm 修改配置 se ...

  2. 「问题思考」python的递归中return返回none

    代码: #求最大公约数 def gcd(x,y): if x < y: swap = x x = y y = swap if x%y == 0: return y else: gcd(y,x%y ...

  3. pwnable.kr cmd1之write up

    看一下源代码: #include <stdio.h> #include <string.h> int filter(char* cmd){ ; r += strstr(cmd, ...

  4. php 复制文件夹

    public function recurse_copy($src,$des) { $dir = opendir($src); @mkdir($des); while(false !== ( $fil ...

  5. 分享14个很酷的jQuery导航菜单插件

    导航按钮是网站的非常重要的一部分,因其将网站的所有部分而集中一处,jQuery导航菜单插件在其中扮演重要的角色. 本文介绍了14个很酷的jQuery导航菜单插件,它们够漂亮.简单,并且完全兼容各种类型 ...

  6. 【转】玩玩负载均衡---在window与linux下配置nginx

    最近有些时间,开始接触负载均衡方面的东西,从硬件F5再到Citrix Netscalar.不过因为硬件的配置虽然不复杂,但昂贵的价格也让一般用户望而却步(十几万到几十万),所以只能转向nginx,sq ...

  7. python mock模块使用(二)

    本篇继续介绍mock里面另一种实现方式,patch装饰器的使用,patch() 作为函数装饰器,为您创建模拟并将其传递到装饰函数 官方文档地址 patch简介 1.unittest.mock.patc ...

  8. 如何在非localhost情况下访问Istio中的服务UI

    在使用Istio时经常会遇到需要用localhost访问服务UI才能看到相关的一些数据 但对于远程连接的时候使用localhost并不方便,所以需要修改一下它的部署文件,将原先的cluster IP改 ...

  9. 内存管理(——高质量程序设计语言C/C++第16章)

    内存的分配方式: 1.静态存储区分配:全局变量,static变量等,在程序编译时已经分配了存储内存,在程序运行的整个期间一直存在 2.程序的堆栈上:程序的局部变量,包括程序的形参等,只存在于程序的运行 ...

  10. Pytho操作MySQL

    一.相关代码 数据库配置类 MysqlDBConn.py 01 #encoding=utf-8 02 ''' 03 Created on 2012-11-12 04    05 @author: St ...