Posting array of JSON objects to MVC3 action method via jQuery ajax
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的更多相关文章
- MVC3学习:利用jquery+ajax生成二维码(支持中文)
二维码越来越热火了,做电子商务网站,不做二维码,你就OUT了. 一.下载DLL文件(ThoughtWorks.QRCode.dll),并在项目中引用.点此下载 如果你还不知道什么是QRCode二维码, ...
- 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 ...
- Jquery Ajax方法传递json到action
ajax向后台传入json需要设置option,如下 contentType:'application/json' data:Json.Stringify(jsObj) 后台处理复杂json对象(不知 ...
- python np array转json
np array转json import numpy as np import codecs, json a = np.arange().reshape(,) # a by array b = a.t ...
- 再谈Jquery Ajax方法传递到action 【转载】
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://cnn237111.blog.51cto.com/2359144/984466 之 ...
- 再谈Jquery Ajax方法传递到action
原始出处 :http://cnn237111.blog.51cto.com/2359144/984466 本人只是转载 原文如下: 假设 controller中的方法是如下: public Act ...
- 再谈Jquery Ajax方法传递到action(转)
之前写过一篇文章Jquery Ajax方法传值到action,本文是对该文的补充. 假设 controller中的方法是如下: public ActionResult ReadPerson(Perso ...
- MVC中使用Ajax提交数据 Jquery Ajax方法传值到action
Jquery Ajax方法传值到action <script type="text/javascript"> $(document).ready(function(){ ...
- php+jquery+ajax+json的一个最简单实例
html页面: <html> <head> <meta http-equiv="content-type" content="text/ht ...
随机推荐
- SVN服务器的部署与安装
需要下载并安装VisualSVN,TortoiseSVN,VisualSVN-Server三个工具. 其中VisualSVN是SVN针对VisualStudio的插件: TortoiseSVN是客户端 ...
- Go常量与枚举类型
package main import ( "math" "fmt" ) //常量与枚举 //const数值可作为各种类型使用 func consts() { ...
- CSU1350 To Add which?
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1350 这题目因为每一个数都跟相邻的数有关,所以可以从左到右和从右到左一次扫一遍即可 代 ...
- js获取json属性值的两种方法
1.json.XXX 2.json["XXX"] 第二种方法使用场景,当属性值是变量时.如图所示:
- Pollard rho模板
#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #in ...
- C#.net中当地址有中文时,图片无法显示解决方法
原文发布时间为:2008-11-05 -- 来源于本人的百度文章 [由搬家工具导入] 搞了半天都无法正常显示图片, string path = Server.MapPath("." ...
- 深入理解计算机操作系统——第11章:CS模型,网络
网络编程: 11.1 客户端-服务器编程模型 (1)一个应用是由一个服务器进程和一个或多个客户端进程组成. (2)服务器管理某种资源,并且操纵这种资源来为客户端服务. CS模型: CS的基本操作是事务 ...
- python学习之 - XML
xml模块定义:实现不同语言或程序之间进行数据交换的协议.格式如下:通过<>节点来区别数据结构如:<load-on-startup(这个是标签) test="value&q ...
- 【小记事】解除端口占用(Windows)
开发中有时会因为端口占用而导致起项目时报错(如下图),这时候只要解除端口占用即可. 解除端口占用: 1.打开cmd(win+r),查看端口占用情况 netstat -ano | findstr 端口号 ...
- HDU——2119 Matrix
Matrix Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...