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');
}
});