Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC 4 model coming back null
    primarykey
    data
    text
    <p>So I have a view that requires several different objects and lists of objects to be passed in and out that I have created a viewmodel for. My viewmodel looks like this</p> <pre><code>public class EditUserViewModel { public ManageUsersViewModel ManageUsersViewModel { get; set; } public IEnumerable&lt;StateModel&gt; StateModel { get; set; } } </code></pre> <p>The part I'm having trouble with is the StateModel which looks like this</p> <pre><code>public class StateModel { public bool IsChecked { get; set; } public States States { get; set; } public UsersInStates UsersInStates { get; set; } } </code></pre> <p>and contains this</p> <pre><code>[Table("States")] public class States { [Key] public int StateId { get; set; } public string State { get; set; } } [Table("UsersInStates")] public class UsersInStates { [Key, Column(Order = 1)] public int UserId { get; set; } [Key, Column(Order = 2)] public int StateId { get; set; } public string LicenseNumber { get; set; } } </code></pre> <p>In my view I'm more or less trying to loop through the states and take user input for UsersInStates. This is how I'm trying to accomplish it but my entire StateModel comes back null. Going into the view the StateModel.States has data and the UsersInStates does not. This is what it looks like in my view</p> <pre><code>@foreach (var state in Model.StateModel) { @Html.HiddenFor(m =&gt; state) &lt;tr&gt; &lt;td&gt; @Html.CheckBoxFor(m =&gt; state.IsChecked) &lt;/td&gt; &lt;td&gt; @Html.Label(state.States.State) &lt;/td&gt; &lt;td&gt; @Html.EditorFor(m =&gt; state.UsersInStates.LicenseNumber) &lt;/td&gt; &lt;/tr&gt; } </code></pre> <p>Any advice would be much appreciated. Everything displays as it should and the ManageUsersViewModel part works fine it's just the StateModel data coming back to the controller is null and I'm not exactly sure how to make this work the way I'd like it to.</p> <p>This is what the generated html looks like for the start of the table and the first row as requested</p> <pre><code>&lt;table style="margin-left:auto; margin-right:auto; text-align:center"&gt; &lt;input id="state" name="state" type="hidden" value="WebSiteNew.Models.StateModel" /&gt; &lt;tr&gt; &lt;td&gt; &lt;input data-val="true" data-val-required="The IsChecked field is required." id="state_IsChecked" name="state.IsChecked" type="checkbox" value="true" /&gt; &lt;input name="state.IsChecked" type="hidden" value="false" /&gt; &lt;/td&gt; &lt;td&gt; &lt;label for="Alabama"&gt;Alabama&lt;/label&gt; &lt;/td&gt; &lt;td&gt; &lt;input class="text-box single-line" id="state_UsersInStates_LicenseNumber" name="state.UsersInStates.LicenseNumber" type="text" value="" /&gt; &lt;/td&gt; &lt;/tr&gt; </code></pre> <p><strong>Answer:</strong></p> <p>Ok so to solve this I used a for loop as explained in both references listed in the answer below</p> <pre><code>@for (int i = 0; i &lt; Model.StateModel.Count(); i++) { &lt;tr&gt; &lt;td&gt; @Html.HiddenFor(m =&gt; m.StateModel[i].States.StateId) @Html.HiddenFor(m =&gt; m.StateModel[i].States.State) @Html.CheckBoxFor(m =&gt; m.StateModel[i].IsChecked) &lt;/td&gt; &lt;td&gt; @Html.Label(Model.StateModel[i].States.State) &lt;/td&gt; &lt;td&gt; @Html.EditorFor(m =&gt; m.StateModel[i].UsersInStates.LicenseNumber) &lt;/td&gt; &lt;/tr&gt; } </code></pre> <p>Also a note to anyone looking at this, I had to change IEnumerable in my EditUsersViewModel to IList to allow for indexing.</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