Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC3 Json model binding not working when sent to server
    primarykey
    data
    text
    <p>I'm having a an odd problem with MVC3 and the model binding. When I post a JSON object to my controller the model binder can't create a typed object out of it at all. All the properties are the default (i.e. empty strings)</p> <p>However if I create an instance on the server, and send it as a JSON action result the data on the wire looks identical.</p> <p>I've tried with </p> <pre><code>$.ajaxSettings.traditional = true; </code></pre> <p>and it makes no difference</p> <p>As an example if I post </p> <pre><code>{"RoutineName":"My new routine","Routines":[{"DayName":"Monday","Items":[21,31]}]} </code></pre> <p>The model binder fails, but coming from the server the data looks like</p> <pre><code>{"RoutineName":"Routine From Code","Routines":[{"DayName":"Monday","Items":[1,2]},{"DayName":"Tuesday","Items":[]}]} </code></pre> <p>The html used to generate this looks like</p> <pre><code>$('#submitRoutine').click(function () { var routines = []; $('.DayName').each(function (index, item) { var $item = $(item); var name = $item.html(); var routineItems = []; $($item.attr('href')).find('.itemId').each(function () { routineItems.push(parseInt($(this).val(), 10)); }); routines.push({ DayName: name, Items: routineItems }); }); var routine = { RoutineName: $('#routineName').val(), Routines: routines }; $.ajaxSettings.traditional = true; $.post('/Machine/CreateRoutine', JSON.stringify(routine),function (data) {},'json'); }); </code></pre> <p>So it looks like model binding from a typed object to JSON is ok, but coming back the other way isn't. Is there something I've missed?</p> <p>The models are in F#</p> <pre><code>type RoutineDayViewModel() = let mutable _name = String.Empty let mutable _items = new ResizeArray&lt;int&gt;() member x.DayName with get() = _name and set value = _name &lt;- value member x.Items with get() = _items and set value = _items &lt;- value type RoutineViewModel() = let mutable _name = String.Empty let mutable _routines = new ResizeArray&lt;RoutineDayViewModel&gt;() member x.RoutineName with get() = _name and set value = _name &lt;- value member x.Routines with get() = _routines and set value = _routines &lt;- value </code></pre> <p>EDIT: I've also tried with the following C# classes and get the same result</p> <pre><code> public class RoutineDayViewModel { public string DayName { get; set; } public List&lt;int&gt; Items{ get; set; } } public class RoutineViewModel { public string RoutineName { get; set; } public List&lt;RoutineDayViewModel&gt; Routines { get; set; } } </code></pre> <p>I've also added the following to the global.asax</p> <pre><code>ValueProviderFactories.Factories.Add(new JsonValueProviderFactory()) </code></pre> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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