Note that there are some explanatory texts on larger screens.

plurals
  1. POPost an Array of Objects via JSON to ASP.Net MVC3
    primarykey
    data
    text
    <p>I'm looking for a solution to POSTing an array of objects to MVC3 via JSON.</p> <p>Example code I'm working off of: <a href="http://weblogs.asp.net/scottgu/archive/2010/07/27/introducing-asp-net-mvc-3-preview-1.aspx" rel="nofollow noreferrer">http://weblogs.asp.net/scottgu/archive/2010/07/27/introducing-asp-net-mvc-3-preview-1.aspx</a></p> <p>JS:</p> <pre><code>var data = { ItemList: [ {Str: 'hi', Enabled: true} ], X: 1, Y: 2 }; $.ajax({ url: '/list/save', data: JSON.stringify(data), success: success, error: error, type: 'POST', contentType: 'application/json, charset=utf-8', dataType: 'json' }); </code></pre> <p>ListViewModel.cs:</p> <pre><code>public class ListViewModel { public List&lt;ItemViewModel&gt; ItemList { get; set; } public float X { get; set; } public float Y { get; set; } } </code></pre> <p>ItemViewModel.cs:</p> <pre><code>public class ItemViewModel { public string Str; // originally posted with: { get; set; } public bool Enabled; // originally posted with: { get; set; } } </code></pre> <p>ListController.cs:</p> <pre><code>public ActionResult Save(ListViewModel list) { // Do something } </code></pre> <p>The result of this POST:</p> <p>list is set, to a ListViewModel<br> Its X and Y properties are set<br> The underlying ItemList property is set<br> The ItemList contains one item, as it should<br> The item in that ItemList is uninitialized. Str is null and Enabled is false. </p> <p>Put another way, this is what I get from MVC3's model binding:</p> <pre><code>list.X == 1 list.Y == 2 list.ItemList != null list.ItemList.Count == 1 list.ItemList[0] != null list.ItemList[0].Str == null </code></pre> <p>It would appear the MVC3 JsonValueProvider is not working for complex objects. How do I get this to work? Do I need to modify the existing MVC3 JsonValueProvider and fix it? If so, how do I get at it and replace it in an MVC3 project?</p> <p>Related StackOverflow questions I've already pursued to no avail:</p> <p><a href="https://stackoverflow.com/questions/2203912/asp-net-mvc-ajax-json-post-array">Asp.net Mvc Ajax Json (post Array)</a> Uses MVC2 and older form-based encoding - that approach fails with an object that contains an array of objects (JQuery fails to encode it properly).</p> <p><a href="https://stackoverflow.com/questions/760249/post-an-array-of-complex-objects-with-json-jquery-to-asp-net-mvc-controller">Post an array of complex objects with JSON, JQuery to ASP.NET MVC Controller</a> Uses a hack I'd like to avoid where the Controller instead receives a plain string which it then manually deserializes itself, rather than leveraging the framework.</p> <p><a href="https://stackoverflow.com/questions/4627985/mvc3-rc2-json-post-binding-not-working-correctly">MVC3 RC2 JSON Post Binding not working correctly </a> Didn't have his content-type set - it's set in my code.</p> <p><a href="https://stackoverflow.com/questions/320291/how-to-post-an-array-of-complex-objects-with-json-jquery-to-asp-net-mvc-controll">How to post an array of complex objects with JSON, jQuery to ASP.NET MVC Controller?</a> This poor guy had to write a JsonFilter just to parse an array. Another hack I'd prefer to avoid.</p> <p>So, how do I make this happen?</p>
    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