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
- jquery从tr获取td
- 新学的插件jquery.tablesorter很好用,说说遇到的表格动态添加的数据不能排序的问题
- Jquery dom查找方法 性能分析
- Jquery常用速查(自用)
- jQuery之定时器及图片轮播实例
- jQuery源码分析之整体架构
- jQuery事件
- 定制化jQuery
- [jQueryUI] - Chosen:select下拉选择框美化插件及问题【转】
- reset是js的方法,在jquery中使用应注意
- jquery ajax jsonp 跨域
- jquery-fakeloader插件的使用
- Jquery动画效果
- JQuery攻略读书笔记---第2章 数组
- JQuery 两种方法解决刚创建的元素遍历不到的问题
- jquery跟随屏幕滚动效果的实现代码
- jQuery操作属性和样式详解
- jquery插件开发的几种方式
- jquery.min.js需不需要引入
- 155 jQuery source --1