模型聯編程序是否不支持JSON對象數組?下面的代碼在將單個JSON域對象作爲ajax帖子的一部分發送時工作。但是,在發送JSON域對象數組時,action參數爲null。通過jQuery將JSON對象數組發送到MVC3操作方法ajax
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');
}
});
這是操作方法:
public ActionResult AddDomain(IEnumerable<DomainBasketItemModel> domain)
{
...
任何想法如果能夠做到這一點?
編輯
@Milimetric
你的解決方案的工作!但是,這是我的錯,但是我演示的代碼並不是我的問題的實際代碼,我試圖展示相當易於理解的代碼。
其實我創建一個數組,然後一些作用中的DOM元素,推動JSON對象到數組,然後發佈此陣列作爲數據...
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');
}
});
相關 - http://stackoverflow.com/questions/2515773/ajax-post-of-javascript-string-array-to-jsonresult-as -liststring-always-returns – ChrisF 2013-05-09 20:36:15