Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I edit multiple models in ASP.NET MVC using Partial Views?
    text
    copied!<p>I'm new to ASP.NET MVC 4 and I'm struggling with a concept that would be easy in webforms. So, if I have a Customer class, and a Customer has an Address, how can I edit both the Customer and Address on the same form in the same submit action? I would like to create an '_Edit' partial view for the Address, but I don't know how to wire the controller up if there is no submit button for the Address. I just want a single button to save all the Models in a single View. </p> <p>So, I could create a new CompanyView model, view, and controller and do it that way. However, if I have many objects having Addresses it seems like a lot of work to keep creating View Models to allow you to edit both the object and the address in the same form. Is there a way to create an Address partial edit view and somehow update the Address fields in the Company Edit Controller? Or, have it somehow pass the model.Address back to the Company Edit Controller instead of null?</p> <p><em>Edit:</em></p> <p><strong>Models</strong></p> <pre><code>public class Address { public Int32 Id { get; set; } [Display(Name = "Address 1")] public String Address1 { get; set; } [Display(Name = "Address 2")] public String Address2 { get; set; } [Display(Name = "City")] public String City { get; set; } [Display(Name = "State")] public String State { get; set; } [Display(Name = "Postal Code")] public String PostalCode { get; set; } [Display(Name = "Country")] public String Country { get; set; } } public class Company { public Int32 Id { get; set; } [Display(Name = "Company Name")] public String Name { get; set; } public Int32 AddressId { get; set; } public virtual Address Address { get; set; } } </code></pre> <p><strong>Address _Edit Partial View</strong></p> <pre><code>@model Models.Address &lt;div class="well"&gt; &lt;fieldset&gt; &lt;legend&gt;Address&lt;/legend&gt; @Html.EditorFor(model =&gt; model.Address1) @Html.EditorFor(model =&gt; model.Address2) @Html.EditorFor(model =&gt; model.City) @Html.EditorFor(model =&gt; model.State) @Html.EditorFor(model =&gt; model.PostalCode) @Html.EditorFor(model =&gt; model.Country) &lt;/fieldset&gt; &lt;/div&gt; </code></pre> <p><strong>Company Edit View</strong></p> <pre><code>@model Models.Company @{ ViewBag.Title = "Edit"; Layout = "~/Views/shared/ContentLayout.cshtml"; } &lt;div class="row"&gt; &lt;div class="col-lg-12"&gt; &lt;div class="page-header"&gt; &lt;h2&gt;Edit Company&lt;/h2&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;div class="col-lg-8"&gt; @using (Html.BeginForm("Edit", "Company", new { @class = "bs-example form-horizontal" })) { @Html.AntiForgeryToken() @Html.ValidationSummary(true) @Html.HiddenFor(model =&gt; model.Id) @Html.EditorFor(model =&gt; model.Name) @Html.HiddenFor(model =&gt; model.AddressId) @Html.Partial("~/Views/Address/_Edit.cshtml", Model.Address) &lt;p&gt; &lt;button name="button" type="submit" class="btn btn-primary" value="submit"&gt;Submit&lt;/button&gt; &lt;/p&gt; } &lt;/div&gt; &lt;/div&gt; </code></pre> <p><strong>Company Edit Controller</strong></p> <pre><code>[HttpPost] public ActionResult Edit(Company model, int id) { if (ModelState.IsValid) { // model.Address = NULL here! Success("Record updated!"); return RedirectToAction("Index"); } } </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