Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC 3 One-To-Many Form
    text
    copied!<p>I have this classes who is mapped using Entity Framework Code First:</p> <pre><code>public class Person { public int PersonId { get; set; } public string Name { get; set; } //... additional properties [Required] public virtual ICollection&lt;Address&gt; Addresses { get; set; } } public class Address { public int AddressId { get; set; } public string Street { get; set; } //... additional properties public int PersonId { get; set; } public virtual Person Person { get; set; } } </code></pre> <p>So, using ASP.NET MVC 3, how can I perform the Edit View for Person to fill all Person properties within an Address in just one Form?</p> <pre><code>@using (Html.BeginForm()) { @Html.ValidationSummary(true) &lt;fieldset&gt; &lt;legend&gt;Person&lt;/legend&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Name) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Name) @Html.ValidationMessageFor(model =&gt; model.Name) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Adresses.FirstOrDefault().Street) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Adresses.FirstOrDefault().Street) @Html.ValidationMessageFor(model =&gt; model.Adresses.FirstOrDefault().Street) &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Create" /&gt; &lt;/p&gt; &lt;/fieldset&gt; } </code></pre> <p>This works fine to show the inputs with Street from database, but when I do the submit, the Street doesnt change.</p> <pre><code>[HttpPost] public ActionResult Edit(int id, FormCollection formCollection) { Person person = unityOfWork.PersonRepository.GetById(id); UpdateModel&lt;Person&gt;(person); if (ModelState.IsValid) { unityOfWork.PersonRepository.Update(person); unityOfWork.SaveChanges(); return RedirectToAction("Index"); } return View(person); } </code></pre>
 

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