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 ...
随机推荐
- Highlights in a Journal
** Highlights **** example- b huang, 2016, Design and performance enhancement of a bi-directional co ...
- 如何在ASP.NET MVC为Action定义筛选器
在ASP.NET MVC中,经常会用到[Required]等特性,在MVC中,同样可以为Action自定义筛选器,来描述控制器所遵守的规则. 首先,我们在ASP.NET MVC项目中定义一个TestC ...
- 大数据学习——yum练习安装mysql
1. 安装mysql 服务器端: yum install mysql-server yum install mysql-devel 2. 安装mysql客户端: yum install mysql 3 ...
- python协程有多厉害?
爬一个××网站上的东西,测算了一下协程的速度提升到底有多大,网站链接就不放了... import requests from bs4 import BeautifulSoup as sb import ...
- POJ 1273 Drainage Ditches【图论,网络流】
就是普通的网络流问题,想试试新学的dinic算法,这个算法暑假就开始看国家集训队论文了,之前一直都只用没效率的EK算法,真正学会这个算法还是开学后白书上的描述:dinic算法就是不断用BFS构建层次图 ...
- hdu 2845
#include<stdio.h> #define N 200100 int f[N]; int a[N],n; int main() { int m,j,i,suma,sumb,sum ...
- Codevs 1497 取余运算== 洛谷P 1226
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 输入b,p,k的值,编程计算bp mod k的值.其中的b,p,k*k ...
- Java开发一些小的思想与功能小记(二)
1.用if+return代替复杂的if...else(if+return) public static void test1(String str) { if ("1".equal ...
- visual svn 搭建
详细出处参考:http://www.jb51.net/article/17365.htm 这里提示一个需要注意的地方: 在签入源代码到SVN服务器的时候: 点击Import,弹出下面的窗体(图2-2- ...
- msp430项目编程17
msp430中项目---红外遥控系统 1.定时器工作原理 2.电路原理说明 3.代码(显示部分) 4.代码(功能实现) 5.项目总结 msp430项目编程 msp430入门学习