Note that there are some explanatory texts on larger screens.

plurals
  1. POCollection data not appearing in models after being posted. (Includes collections of collections.)
    primarykey
    data
    text
    <p>I have a PersonEditorModel that contains a list of people that I want represented in the editor. I have a collection of Person objects that contains a collection of Address objects. I want to render text boxes for all of these so that the user can edit names and address associated with those names. </p> <pre><code>class PersonEditorModel { public List&lt;Person&gt; People; } class Person { public string Name; public List&lt;Address&gt; Addresses; } class Address { public string Value; } </code></pre> <p>I'm generating a form for editing the addresses and the names associated with them. It's using Ajax.BeginForm because this is a simplified example derived from a problem I'm having in a bigger app where the form updates a different part of the page using Ajax to get a result.</p> <p>In the base form's cshtml: </p> <pre><code>@model Models.PersonEditorModel @using (Ajax.BeginForm("Update", new AjaxOptions { HttpMethod = "Post" })) { &lt;div&gt; @Html.EditorFor(x =&gt; x.People) &lt;button type="submit"&gt;Commit Changes&lt;/button&gt; &lt;/div&gt; } </code></pre> <p>Person.cshtml:</p> <pre><code>@model Models.Person &lt;div&gt; @Html.TextBoxFor(x =&gt; x.Name) &lt;/div&gt; &lt;div&gt; @Html.EditorFor(x =&gt; x.Addresses) &lt;/div&gt; </code></pre> <p>Address.cshtml:</p> <pre><code>@model Models.Address &lt;div&gt; @Html.TextBoxFor(x =&gt; x.Value) &lt;/div&gt; </code></pre> <p>The controller's method for the action:</p> <pre><code>public ActionResult Update(List&lt;Person&gt; people) { /* snip */ } </code></pre> <p>When I submit this form, a breakpoint placed immediately within Update() shows that "people" is a list of the right length but it contains absolutely no data--all the addresses are empty strings, even though the form data as appears in Request.Form looks correct.</p> <p>What could be causing such a problem and what would be an idiomatic MVC way to solve this issue?</p> <p>NOTE: This is a simplified example of some behavior I'm seeing in a more complex app. I think I've captured the essence of the issue here, but there may be some other unknown complicating factor. Let me know what could complicate this to cause the issue I'm seeing and I'll try to provide whatever additional details I can. </p> <p>I've tried making Update take a PersonEditorModel parameter instead, but that gets absolutely no data at all.</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