Note that there are some explanatory texts on larger screens.

plurals
  1. POAsp:net MVC 3: @Html.EditorFor a subcollection of my model in a template?
    primarykey
    data
    text
    <p>I've been stuck a long time to edit a subcollection of my model, the collection of the model was coming null.</p> <p>I finally found a solution, but I find it a little dirty:</p> <p>First my tests datas:</p> <p><strong>Model object</strong>:</p> <pre><code> public class ContainerObject { public String Title { get; set; } public List&lt;ContainedObject&gt; ObjectList { get; set; } } </code></pre> <p><strong>Sub collection object</strong>:</p> <pre><code>public class ContainedObject { public int Id { get; set; } public String Text { get; set; } public Boolean IsSelected { get; set; } } </code></pre> <p><strong>Controller method which generate the object</strong></p> <pre><code> public ActionResult TestForm() { return View(new ContainerObject() { Title = "This is a sample title", ObjectList = new List&lt;ContainedObject&gt;() { new ContainedObject(){Id=1, IsSelected = true, Text="ObjectOne"}, new ContainedObject(){Id=2, IsSelected = false, Text="ObjectTwo"}, new ContainedObject(){Id=3, IsSelected = true, Text="ObjectThree"}, new ContainedObject(){Id=4, IsSelected = false, Text="ObjectFour"}, } }); } </code></pre> <p><strong>Controller which receive the edited object</strong></p> <pre><code> [HttpPost] public ActionResult TestFormResult(ContainerObject filledObject) { return View(); } </code></pre> <p><strong>The view</strong></p> <pre><code>@model WebTestApplication.Models.ContainerObject @{ ViewBag.Title = "TestForm"; } @using (Html.BeginForm("TestFormResult","Home", FormMethod.Post)){ @Html.EditorFor(x =&gt; x.Title) Html.RenderPartial("ContainedObject", Model.ObjectList); &lt;input type="submit" value="Submit"/&gt; } </code></pre> <p><strong>The partial view(ContainedObject.cshtml)</strong></p> <pre><code>@model IEnumerable&lt;WebTestApplication.Models.ContainedObject&gt; @{ ViewBag.Title = "ContainedObject"; int i = 0; } @foreach (WebTestApplication.Models.ContainedObject currentObject in Model) { &lt;br /&gt; @Html.Label(currentObject.Text); @Html.CheckBox("ObjectList[" + i + "].IsSelected", currentObject.IsSelected); @Html.Hidden("ObjectList[" + i + "].Id", currentObject.Id); @Html.Hidden("ObjectList[" + i + "].Text", currentObject.Text); i++; } </code></pre> <p>This is actually working, but I've one problem:</p> <ul> <li>I've to generate names myself and specify the property of the container object</li> </ul> <p>I tried to use <code>Html.EditorFor</code> instead of <code>Html.RenderPartial</code> in the view, the problem is that it generate me the name "ObjectList.[0].Id"(with a additional . between the property name and the accessor).</p> <p>I also tried to use only @Html.EditorFor in the partial view, but it create vars with the name of the object.</p> <p>If I don't use any template, it works:</p> <pre><code> @model WebTestApplication.Models.ContainerObject @{ ViewBag.Title = "TestForm"; } @using (Html.BeginForm("TestFormResult", "Home", FormMethod.Post)) { @Html.EditorFor(x =&gt; x.Title) for (int i = 0; i &lt; Model.ObjectList.Count; i++) { &lt;br /&gt; @Html.Label(Model.ObjectList[i].Text); @Html.CheckBoxFor(m =&gt; Model.ObjectList[i].IsSelected); @Html.HiddenFor(m =&gt; Model.ObjectList[i].Id); @Html.HiddenFor(m =&gt; Model.ObjectList[i].Text); } &lt;br /&gt;&lt;input type="submit" value="Submit"/&gt; } </code></pre> <p>But here it's a simple template, but in my real case, I will have much more data, and this will be re-used multiple time. So what is my best option?</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