Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Darin's answer is correct, but there is a caveat: you need to have a field in your form for each "set" field in the model, otherwise it isn't a match and the resulting List of model is null.</p> <p>Consider this case for a List of AccountModel:</p> <pre><code>public class AccountModel { public AccountType Type { get; set; } public bool Selected { get; set; } public string Name { get { return Util.GetAccountTypeName(Type); } } } </code></pre> <p>The parent view need only contain ...</p> <pre><code> @Html.EditorFor(model =&gt; model.Accounts) </code></pre> <p>... where ...</p> <pre><code> List&lt;AccountModel&gt; Accounts { get; set; } </code></pre> <p>... is part of your overall ViewModel.</p> <p>Now, if you create your editor template like this ...</p> <pre><code>@model WebLinx.Models.AccountModel &lt;div class="informationRow"&gt; @Html.CheckBoxFor(x =&gt; x.Selected) &lt;div class="formLabel"&gt; @Html.Label(Model.Name) &lt;/div&gt; &lt;/div&gt; </code></pre> <p>... then you will get a null object back in your parent model when you submit, even though the list of accounts with checkboxes view appears complete when displayed.</p> <p>But if you include one extra line:</p> <pre><code>@model WebLinx.Models.AccountModel &lt;div class="informationRow"&gt; @Html.CheckBoxFor(x =&gt; x.Selected) @Html.HiddenFor( x=&gt; x.Type) @* extra line with hidden field *@ &lt;div class="formLabel"&gt; @Html.Label(Model.Name) &lt;/div&gt; &lt;/div&gt; </code></pre> <p>then you have a complete match between the form and the AccountModel object, and the List of AccountModel will contain all the members (and updated values for the checkboxes).</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      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