Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.Net MVC Posted form values not mapping back onto viewmodel in controller
    text
    copied!<p>My ViewModel is:</p> <pre><code>public class ObjectiveVM { public string DateSelected { get; set; } public List&lt;string&gt; DatePeriod { get; set; } public IList&lt;ObList&gt; obList { get; set; } public class ObList { public int ObjectiveId { get; set; } public int AnalystId { get; set; } public string Title { get; set; } public string Description { get; set; } public string AnalystName { get; set; } public bool Include { get; set; } } } </code></pre> <p>This is passed to the view, populated as expected - and displays correctly in the view.</p> <p>My problem is when it is posted back to the controller. My controller code to accept it back is:</p> <pre><code>[HttpPost] [ValidateAntiForgeryToken] public ActionResult Analyst(ObjectiveVM ovm) </code></pre> <p><code>ovm.obList</code> is always showing as null:</p> <p><img src="https://i.stack.imgur.com/vanUi.png" alt="Showing posted values"></p> <p>My View html is:</p> <pre><code>@model Objectives.ViewModels.ObjectiveVM @{ ViewBag.Title = "Analyst"; } &lt;h2&gt;Copy Objectives for Analyst&lt;/h2&gt; @using (Html.BeginForm()) { @Html.AntiForgeryToken() @Html.ValidationSummary(true) &lt;fieldset&gt; &lt;legend&gt;Objective&lt;/legend&gt; @Html.DropDownListFor(model =&gt; model.DateSelected, new SelectList(Model.DatePeriod)) &lt;table&gt; &lt;tr&gt; &lt;th&gt; @Html.DisplayNameFor(model =&gt; model.obList[0].Include) &lt;/th&gt; &lt;th&gt; @Html.DisplayNameFor(model =&gt; model.obList[0].AnalystName) &lt;/th&gt; &lt;th&gt; @Html.DisplayNameFor(model =&gt; model.obList[0].Title) &lt;/th&gt; &lt;th&gt; @Html.DisplayNameFor(model =&gt; model.obList[0].Description) &lt;/th&gt; &lt;/tr&gt; @foreach (var obList in Model.obList) { &lt;tr&gt; &lt;td&gt; @Html.HiddenFor(modelItem =&gt; obList.ObjectiveId) @Html.HiddenFor(modelItem =&gt; obList.AnalystId) @Html.HiddenFor(modelItem =&gt; obList.Title) @Html.HiddenFor(modelItem =&gt; obList.Description) @Html.CheckBoxFor(modelItem =&gt; obList.Include) &lt;/td&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; obList.AnalystName) &lt;/td&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; obList.Title) &lt;/td&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; obList.Description) &lt;/td&gt; &lt;/tr&gt; } &lt;/table&gt; &lt;p&gt; &lt;input type="submit" value="Copy Selected Objectives" /&gt; &lt;/p&gt; &lt;/fieldset&gt; } @section Scripts { @Scripts.Render("~/bundles/jqueryval") } </code></pre> <p>Looking in Developer Tools at the Posted form values, they appear to be ok: <img src="https://i.stack.imgur.com/Yomfw.png" alt="Developer Tools"></p> <p>Can anyone see any reason the posted form values, are not mapping back onto my viewmodel in the Controller HTTP post?</p> <p>Thank you, Mark</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