JSON的发帖阵列通过jQuery AJAX反对MVC3操作方法 [英] Posting array of JSON objects to MVC3 action method via jQuery ajax
问题描述
难道模型绑定不能支持JSON对象的数组?在code以下发送一个JSON域对象的AJAX后零件时的作品。然而,发送JSON域对象的数组时,动作参数为null。
VAR域= {[
域名:testt1,
价格:'19 0.99,
可用:真
},{
域名:testt2,
价格:'15 0.99,
可用:假的
}];
$阿贾克斯({
键入:POST,
网址:Url.BasketAddDomain,
数据类型:JSON,
数据:域名,
成功:函数(basketHtml){
},
错误:功能(A,B,C){
警报('的问题ocurred');
}
});
这是操作方法:
公众的ActionResult AddDomain(IEnumerable的< DomainBasketItemModel>域)
{
...
任何想法如果能够做到这一点?
修改
@Milimetric
您解决方案的工作!但是,这是我的错,而是code我证明不是我的问题的真正code,我是想表现出相当的code,它是更容易理解。
其实我创建一个数组,然后作用中的一些DOM元素,推动JSON对象到数组,然后张贴这个数组作为数据...
VAR域= [];
$(本)。家长('表')找到('输入:选中')。每个(函数(){
VAR域= {
域名:。('名')$(本).parent()父()找到HTML()。
售价:$(本).parent()父()找到('。价格跨度)HTML()
可用:。$(本).parent()父()找到('。可')HTML()==可用
}
domains.push(域);
});
$阿贾克斯({
键入:POST,
网址:Url.BasketAddDomain,
数据类型:JSON,
数据:{域:域},
成功:函数(basketHtml){
},
错误:功能(A,B,C){
警报('的问题ocurred');
}
});
您需要:
VAR域= {域:[...您的内容...]};
$阿贾克斯({
类型:'后',
网址:'你-URI,
数据:JSON.stringify(畴),
的contentType:应用/ JSON的;字符集= UTF-8,
传统:真正的,
成功:功能(数据){
...
}
});
在一般情况下,检查了Request对象中的调试器,你会看到什么东西被传递,并获得如何说话HTTP的想法。
注意:刚刚发现这一点。模型绑定扼流圈可为空的小数属性,如:
公共小数?北纬{获得;组; }
所以不会绑定,如果你张贴到它看起来像这样一个JSON字符串:
{纬度:12.0}
但是,如果你发布这样的事情,将工作:
{纬度:12.0}
请参阅:<一href="http://syper-blogger.blogspot.com/2011/07/hello-world.html">http://syper-blogger.blogspot.com/2011/07/hello-world.html
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?
EDIT
@Milimetric
Your solution works! However, this is my fault, but the code I demonstrated isn't the real code of my problem, I was trying to show equivalent code that is easier to understand.
I am actually creating an array, then interating some DOM elements and pushing a JSON object onto the array, then posting this array as the data...
var domains = [];
$(this).parents('table').find('input:checked').each(function () {
var domain = {
DomainName: $(this).parent().parent().find('.name').html(),
Price: $(this).parent().parent().find('.price span').html(),
Available: $(this).parent().parent().find('.available').html() == "Available"
}
domains.push(domain);
});
$.ajax({
type: 'POST',
url: Url.BasketAddDomain,
dataType: "json",
data: { domain: domains },
success: function (basketHtml) {
},
error: function (a, b, c) {
alert('A problem ocurred');
}
});
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
这篇关于JSON的发帖阵列通过jQuery AJAX反对MVC3操作方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!