Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's how you could proceed:</p> <pre><code>@model MyProject.WebUI.Models.RolesModel @using (Html.BeginForm()) { &lt;p&gt; @Html.LabelFor(m =&gt; m.Name) @Html.EditorFor(m =&gt; m.Name) &lt;/p&gt; &lt;div&gt; &lt;ul&gt; @Html.EditorFor(m =&gt; m.Roles) &lt;/ul&gt; &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Save" /&gt; &lt;/p&gt; } </code></pre> <p>and inside the EditorTemplate (<code>/Views/Shared/EditorTemplates/SelectListItem.cshtml</code>):</p> <pre><code>@model System.Web.Mvc.SelectListItem &lt;h3&gt;Type: @Html.LabelFor(m =&gt; m)&lt;/h3&gt; &lt;li&gt; @Html.CheckBoxFor(m =&gt; m.Selected) @Html.LabelFor(m =&gt; m.Selected, Model.Text) @Html.HiddenFor(m =&gt; m.Value) &lt;/li&gt; </code></pre> <p>Notice the simplification of the editor template. It no longer takes a <code>List&lt;SelectListItem&gt;</code> as model but simply a <code>SelectListItem</code>. It will automatically be invoked for each element of the Roles collection so that you don't need to write any loops. Just follow the conventions.</p> <p>I would also simplify your view model like this:</p> <pre><code>public class RolesModel { public string Name { get; set; } public IEnumerable&lt;SelectListItem&gt; Roles { get; set; } } </code></pre> <p>and your controller:</p> <pre><code>public class TestController : Controller { public ActionResult Edit() { var myModel = new RolesModel { Name = "Joe Bloggs", Roles = new[] { new SelectListItem { Value = "1", Text = "Member", Selected = true }, new SelectListItem { Value = "2", Text = "Manager", Selected = true }, new SelectListItem { Value = "3", Text = "Administrator", Selected = false } } }; return View(myModel); } [HttpPost] public ActionResult Edit(RolesModel m) { // m.Roles should be correctly bound return View("Results", m); } } </code></pre>
    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. 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