Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC2 sending collections from the view a controller via json
    text
    copied!<p>I've been looking on forums for 2 days now and can't find a good answer so I'll just post it.</p> <p>I appear to be having a problem posting JSON back to the controller to save. The JSON should map to model view but it keeps getting default(constructor)values rather then the values from the POST.</p> <p>We have a series of JS widgets that contain a data field with json in them. We do all our data manipulation in these widget objects on the client side. When a user wants to save we grab the data we need from the widgets involved and we put it into another JSON object that matches a ViewModel and POST that back to the server.</p> <p>For example:</p> <pre><code> $("#Save").click(function () { if (itemDetails.preparedForSubmit() &amp;&amp; itemConnections.preparedForSubmit()) { itemComposite.data.Details = itemDetails.data; itemComposite.data.Connections= itemConnections.data; $.post(MYURL, itemComposite.data); } else { alert("failed to save"); } }); </code></pre> <p>The preparedForSubmit() method simple does stuff like any validation checks or last minute formatting you might need to do client side.</p> <p>The itemDetails widgets data matches a ViewModel.</p> <p>The itemConnections widgets data matches a collection of ViewModels.</p> <p>The Controller looks like this:</p> <pre><code> [HttpPost] virtual public JsonResult SaveItemDetailsComposite(ItemComposite inItemData) { if (ModelState.IsValid) { try { _Mapper.Save(itemComposite.Details , itemComposite.Connections); return Json(true); } catch (Exception ex) { _log.Error("Exception " + ex.InnerException.Message); throw; } } return Json(SiteMasterUtilities.CreateValidationErrorResponse(ModelState)); } </code></pre> <p>The ItemComposite Class is a simple View Model that contains a single itemDetails object and a collection of itemConnections. When it returns data to here it is just getting the default data as if it got a new ItemComposite rather than converting the POST data.</p> <p>in Firebug I see the data is posted. Although it looks weird not automatically formatted in firebug.</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