Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I edit a related object's properties in MVC4?
    primarykey
    data
    text
    <p>I'm having some problems editing the properties of a related object. Here is the code:</p> <p><strong>Model:</strong></p> <p><em><strong>DocumentLine.cs</em></strong></p> <pre><code>/// &lt;summary&gt; /// States the base implementation for all document lines in a purchasing module. /// &lt;/summary&gt; public class DocumentLine : Keyed { // ... some other properties /// &lt;summary&gt; /// Gets or sets the current line's document header. /// &lt;/summary&gt; [Navigation] [Display(ResourceType = typeof(Resources.ApplicationResources), Name = "Header")] public virtual DocumentHeader Header { get; set; } } </code></pre> <p><em><strong>DocumentHeader.cs</em></strong></p> <pre><code>/// &lt;summary&gt; /// States the base implementation for all document headers in a purchasing module. /// &lt;/summary&gt; public class DocumentHeader : Keyed { /// &lt;summary&gt; /// Gets or sets the current header's document number. /// &lt;/summary&gt; [Required] [Display(ResourceType = typeof(Resources.ApplicationResources), Name = "DocumentNumber")] public string DocumentNumber { get; set; } /// &lt;summary&gt; /// Gets or sets the extra cost of the document. /// &lt;/summary&gt; [Display(ResourceType = typeof(Resources.ApplicationResources), Name = "ExtraCost")] [RegularExpression(@"^\d*$", ErrorMessageResourceType=typeof(Resources.ApplicationResources), ErrorMessageResourceName= "Exception_ExtraCost_Error")] public decimal ExtraCost { get; set; } /// &lt;summary&gt; /// Gets or sets the vat's extra cost of the document. /// &lt;/summary&gt; [Display(ResourceType = typeof(Resources.ApplicationResources), Name = "ExtraVat")] [RegularExpression(@"^\d*$", ErrorMessageResourceType = typeof(Resources.ApplicationResources), ErrorMessageResourceName = "Exception_ExtraVat_Error")] public decimal ExtraVat { get; set; } /// &lt;summary&gt; /// Gets or sets the navigation property to all dependant Document lines. /// &lt;/summary&gt; [Required] [Navigation] [Display(ResourceType = typeof(Resources.ApplicationResources), Name = "DocumentLines")] public virtual ICollection&lt;DocumentLine&gt; DocumentLines { get; set; } } </code></pre> <p><strong>View:</strong></p> <pre><code>@Html.HiddenFor(model =&gt; model.Header.Id, Model.Header != null ? Model.Header.Id : null) &lt;div class="display-label"&gt; @Html.DisplayNameFor(model =&gt; model.Header.ExtraCost) &lt;/div&gt; &lt;div class="display-field"&gt; &lt;input type="text" name="Header.ExtraCost" id="Header.ExtraCost" data-varname="header.extraCost" value="@(Model.Header.ExtraCost)" /&gt; @Html.ValidationMessageFor(model =&gt; model.Header.ExtraCost) &lt;/div&gt; &lt;div class="display-label"&gt; @Html.DisplayNameFor(model =&gt; model.Header.ExtraVat) &lt;/div&gt; &lt;div class="display-field"&gt; &lt;input type="text" name="Header.ExtraVat" id="Header.ExtraVat" data-varname="header.extraVat" value="@(Model.Header.ExtraVat)" /&gt; @Html.ValidationMessageFor(model =&gt; model.Header.ExtraVat) &lt;/div&gt; </code></pre> <p>I am aware that MVC tracks the ids and names of the inputs to pass the values to the controller, that is why I put the hidden input for the <code>Header.Id</code> value. This view <strong>correctly shows the values</strong> so I do not think the problem is in here.</p> <p><strong>Controller:</strong></p> <p>I have a generic controller method to edit which works fine, although I may have to override it for this particular case.</p> <pre><code>/// &lt;summary&gt; /// Handles the POST event for the Edit action, updating an existing TEntity object. /// &lt;/summary&gt; /// &lt;param name="id"&gt;Id of the TEntity object to update.&lt;/param&gt; /// &lt;param name="model"&gt;TEntity object with properties updated.&lt;/param&gt; /// &lt;returns&gt;Redirection to the Index action if succeeded, the Edit View otherwise.&lt;/returns&gt; [HttpPost] public virtual ActionResult Edit(string id, TEntity model) { var request = new RestSharp.RestRequest(Resource + "?id={id}", RestSharp.Method.PUT) { RequestFormat = RestSharp.DataFormat.Json } .AddParameter("id", id, RestSharp.ParameterType.UrlSegment) .AddBody(model); var response = Client.Execute(request); // Handle response errors HandleResponseErrors(response); if (Errors.Length == 0) return RedirectToAction("Index"); else { ViewBag.Errors = Errors; return View(model); } } </code></pre> <p>The main problem is that this code is not only not editing the related object values, but <strong>also sets to null the <code>Header.Id</code> value of the DocumentLine.</strong></p> <p>Any advice?</p>
    singulars
    1. This table or related slice is empty.
    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.
    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