我使用的是json MVC3,按照at中支持的特性列表,我应该能够获得默认的ASP.Net模型绑定。但是,我还没有成功地将数组/集合从json绑定到action方法参数。尽管我确实让简单的json对象绑定工作正常。如果这里的专家能告诉我我做错了什么,我将不胜感激。
代码如下:
服务器端代码优先:
//Action方法
public JsonResult SaveDiscount(IList<Discount> discounts)
{
foreach(var discount in discounts)
{
....
}
}
//查看模型
public class Discount
{
string Sku{get; set;}
string DiscountValue{get; set;}
string DiscountType{get; set;}
}
//客户端(jquery/js):
var discount = {};
var jsondatacoll = [];
$('#discountgrid tr').each(function () {
sku = $(this).find("td").eq(1).html();
discValue = $(this).find('.discval').val();
discType = $(this).find('.disctype').val();
discount = { Sku: sku, DiscountType: discType, DiscountValue: discValue};
jsondatacoll.push(discount);
}
})
if (jsondatacoll.length > 0) {
var catalogDiscount = JSON.stringify(jsondatacoll);
$.ajax(
{
url: '/url/savediscount',
type: 'POST',
data: catalogDiscount,
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function (data, textStatus, jqXHR) {
...
},
error: function (objAJAXRequest, strError) {
...
}
}
); //ajax
}
我确实检查了fiddler中的json负载,它看起来如下所示:
[
{"Sku":"sku1","DiscountType":"type1","DiscountValue":"10"},
{"Sku":sku2","DiscountType":"type1","DiscountValue":"12"},
{"Sku":"sku3","DiscountType":"type2","DiscountValue":"40"}
]
在服务器端,我确实看到IList<Discount>
折扣中填充了3个空的Discount
对象-这意味着属性为空,但折扣参数的长度为3。
https://stackoverflow.com/questions/5284613
复制相似问题