Note that there are some explanatory texts on larger screens.

plurals
  1. POPosting array of JSON objects to MVC3 action method via jQuery ajax
    primarykey
    data
    text
    <p>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.</p> <pre><code> 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'); } }); </code></pre> <p>This is the action method:</p> <pre><code>public ActionResult AddDomain(IEnumerable&lt;DomainBasketItemModel&gt; domain) { ... </code></pre> <p>Any ideas if it is possible to do this?</p> <p><strong>EDIT</strong></p> <p>@Milimetric</p> <p>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.</p> <p>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...</p> <pre><code>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'); } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload