Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC - Problem with DataBinding to a Collection using a Custom Template
    primarykey
    data
    text
    <p>I am trying to bind to a Model that has a collection property, specifically a List. For the purposes of this example, this represents a list of user roles:</p> <pre><code>public class RolesModel { private List&lt;SelectListItem&gt; _Roles = null; public string Name { get; set; } public List&lt;SelectListItem&gt; Roles { get { if (_Roles == null) { _Roles = new List&lt;SelectListItem&gt;(); } return _Roles; } set { _Roles = value; } } } </code></pre> <p>I am binding this to a strongly-typed view via the following Controller:</p> <pre><code>public class TestController : Controller { RolesModel myModel = new RolesModel(); [HttpGet] public ActionResult Edit() { myModel.Name = "Joe Bloggs"; myModel.Roles = new List&lt;SelectListItem&gt; { 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 is always empty !!! return View("Results", m); } } </code></pre> <p>This then invokes the following view:</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; @Html.EditorFor(m =&gt; m.Roles, "CheckBoxList") &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Save" /&gt; &lt;/p&gt; } </code></pre> <p>Note the template specific call to my custom editor template in '/Views/Shared/EditorTemplates/CheckBoxList.cshtml' this looks like this:</p> <pre><code>@model List&lt;System.Web.Mvc.SelectListItem&gt; &lt;h3&gt;Type: @Html.LabelFor(m =&gt; m)&lt;/h3&gt; &lt;ul&gt; @for (int i = 0; i &lt; Model.Count; i++) { &lt;li&gt; @Html.CheckBoxFor(m =&gt; m[i].Selected) @Html.LabelFor(m =&gt; m[i].Selected, Model[i].Text) @Html.HiddenFor(m =&gt; m[i].Value) &lt;/li&gt; } &lt;/ul&gt; </code></pre> <p>The idea being that each SelectListItem is represented by the Html rendered by the loop.</p> <p>The first part of the process appears to work correctly, The form is presented as expected and you can update the 'Name' text box and the check/uncheck the checkboxes.</p> <p>The problem is that when the form is posted back to the controller, the Roles collection is never populated.</p> <p>I'm new to MVC and thought that the framework actually re-constructed the model data from the post via the enforced form element naming convention. I'm obviously missing an important point and I'm hoping someone can point me in the right direction.</p> <p>Thanks, and apologies for the long post.</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.
 

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