Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Start by fixing your view model and removing the custom constructor or the default model binder won't be able to instantiate it and you will have to write custom model binders and stuff:</p> <pre><code>public class BoolSetting { public string DisplayName { get; set; } public bool Value { get; set; } } public class MyViewModel { public List&lt;BoolSetting&gt; Settings { get; set; } } </code></pre> <p>Then write a controller action that will populate your view model:</p> <pre><code>public class HomeController : Controller { public ActionResult Index() { var model = new MyViewModel { Settings = new[] { new BoolSetting { DisplayName = "name 1", Value = true }, new BoolSetting { DisplayName = "name 2", Value = false }, new BoolSetting { DisplayName = "name 3", Value = true }, }.ToList() }; return View(model); } [HttpPost] public ActionResult Index(MyViewModel model) { return View(model); } } </code></pre> <p>then a view (<code>~/Views/Home/Index.cshtml</code>) in which you simply use editor templates and don't write any <code>foreach</code> loops or weakly typed html helpers such as <code>Html.CheckBox</code>. By using editor templates you will ensure that all input fields will have correct names so that the default model binder is able to fetch their values into the view model during the postback:</p> <pre><code>@model MyViewModel @using (Html.BeginForm()) { @Html.EditorFor(x =&gt; x.Settings) &lt;button type="submit"&gt;OK&lt;/button&gt; } </code></pre> <p>and finally the corresponding editor template for the view model which will be rendered for each element of the collection (<code>~/Views/Home/EditorTemplates/BoolSetting.cshtml</code>):</p> <pre><code>@model BoolSetting &lt;div&gt; @Html.CheckBoxFor(x =&gt; x.Value) @Html.LabelFor(x =&gt; x.Value, Model.DisplayName) @Html.HiddenFor(x =&gt; x.DisplayName) &lt;/div&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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