Note that there are some explanatory texts on larger screens.

plurals
  1. POJson and ASP.NET MVC Model Binding
    text
    copied!<p>I have a UI that looks like this:</p> <p><img src="https://i.stack.imgur.com/d3YFA.png" alt="enter image description here"></p> <p>I am trying to data of the newly created row to the server so that the server may save it.</p> <p>I am sending data in JSON format from the client to my MVC application. Here's my ajax request:</p> <pre><code>var values = []; // an array with each item being an object/associative array // more code to get values into variables for (var i = 0; i &lt; cultures.length; i++) { var cultureName = cultures[i]; var valueTextBox = $(row).find(...); var value = $(valueTextBox).val(); var cultureNameAndValue = { 'CultureShortName' : cultureName, 'StringValue' : value }; values.push(cultureNameAndValue); } var stringTableRow = { 'ResourceKeyId': resourceKeyId, 'Key': resourceKeyName, 'CategoryId': categoryId, 'CategoryName': categoryName, 'StringValues': values }; var stringified = JSON.stringify({ StringTableRow: stringTableRow }); $.ajax('/Strings/JsonCreateNew', { cache: false, async: false, type: 'POST', contentType: 'application/json; charset=UTF-8', data: stringified, dataType: 'json', error: SaveNewResourceClientSideHandler.OnError, success: SaveNewResourceClientSideHandler.OnSuccess }); </code></pre> <p>Here's the data it sends (as seen in Firebug):</p> <pre><code>{"StringTableRow":{"ResourceKeyId":"","Key":"Foo", "CategoryId":"1","CategoryName":"JavaScript", "StringValues":[ {"CultureShortName":"en-US","StringValue":"Something"}, {"CultureShortName":"fr-FR","StringValue":""}] }} </code></pre> <p>Here's my server side code:</p> <pre><code>public ActionResult JsonCreateNew(StringTableRow row) { // CreateNewStringTableRow(row); // return a success code, new resource key id, new category id // return Json( new { Success = true, ResourceKeyId = row.ResourceKeyId, CategoryId = row.CategoryId }, JsonRequestBehavior.AllowGet); return new EmptyResult(); } </code></pre> <p>And here's the business object that I want my incoming POST'ed data to be bound to:</p> <pre><code>public class StringTableRow { public StringTableRow() { StringValues = new List&lt;CultureNameAndStringValue&gt;(); } public long ResourceKeyId { get; set; } public string Key { get; set; } public long CategoryId { get; set; } public string CategoryName { get; set; } public IList&lt;CultureNameAndStringValue&gt; StringValues { get; set; } } public class CultureNameAndStringValue { public string CultureShortName { get; set; } public string StringValue { get; set; } } </code></pre> <p>Global.asax</p> <pre><code>public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); ValueProviderFactories.Factories.Add(new JsonValueProviderFactory()); } } </code></pre> <p>Problem:</p> <p>The action <code>JsonCreateNew</code> receives an object that is not null but has all properties uninitialized, i.e all nullable properties are null and value properties are at their default values. Therefore, effectively I get no data at all even when the client sends a perfectly valid Json string.</p> <p>Do I need to do custom model binding?</p>
 

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