Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For model binding to work properly, you need to post only a <code>Company</code> back to your controller. Just pass your whole model onto your partial :</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) &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>_Edit</strong></p> <pre><code>@model Models.Company &lt;div class="well"&gt; &lt;fieldset&gt; &lt;legend&gt;Address&lt;/legend&gt; @Html.HiddenFor(model =&gt; model.Address.Id) @Html.EditorFor(model =&gt; model.Address.Address1) @Html.EditorFor(model =&gt; model.Address.Address2) @Html.EditorFor(model =&gt; model.Address.City) @Html.EditorFor(model =&gt; model.Address.State) @Html.EditorFor(model =&gt; model.Address.PostalCode) @Html.EditorFor(model =&gt; model.Address.Country) &lt;/fieldset&gt; &lt;/div&gt; </code></pre> <p><strong>Controller</strong></p> <pre><code>[HttpPost] public ActionResult Edit(Company model, int id) { if (ModelState.IsValid) { // model.Address should now be available Success("Record updated!"); return RedirectToAction("Index"); } } </code></pre> <p>You should now see the <code>Address</code> navigation property of your model properly bound on post.</p> <hr> <p><strong>Edit based on question in comment</strong></p> <p>How you set up your views and partials is up to you really. The important thing to remember is that model binding works based on the names given to the form elements by the HTML helpers. </p> <p>So <code>Html.HiddenFor(m =&gt; m.Id)</code> will result in <code>&lt;input name="Id"&gt;</code> while <code>Html.HiddenFor(m =&gt; m.Address.Id)</code> will result in <code>&lt;input name="Address.Id"&gt;</code>. First one won't be picked up by the model binder as a navigation property of <code>Company</code>, second one will.</p> <p>The simple route would be to just duplicate your partial view. But if it gets to the point where your partial becomes quite large and complex with a high amount of fields, you could create a partial base class that both your entities inherit of.</p> <p>BaseEntityWithAddress.cs</p> <pre><code>public partial class BaseEntityWithAddress { public virtual Address Address { get; set; } } </code></pre> <p>Customer.cs</p> <pre><code>public class Customer : BaseEntityWithAddress { // your properties, no need to redefine Address here } </code></pre> <p>Vendor.cs</p> <pre><code>public class Vendor: BaseEntityWithAddress { // your properties, no need to redefine Address here } </code></pre> <p>And then your partial view would take <code>BaseEntityWithAddress</code> as a model to which you would still pass the whole model.</p> <p>_Edit.cshtml</p> <pre><code>@model Models.BaseEntityWithAddress &lt;div class="well"&gt; &lt;fieldset&gt; &lt;legend&gt;Address&lt;/legend&gt; @Html.HiddenFor(model =&gt; model.Address.Id) @Html.EditorFor(model =&gt; model.Address.Address1) @Html.EditorFor(model =&gt; model.Address.Address2) @Html.EditorFor(model =&gt; model.Address.City) @Html.EditorFor(model =&gt; model.Address.State) @Html.EditorFor(model =&gt; model.Address.PostalCode) @Html.EditorFor(model =&gt; model.Address.Country) &lt;/fieldset&gt; &lt;/div&gt; </code></pre> <p>And that will generate form elements with the correct names for the model binder to pick up.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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