I'm having some problems editing the properties of a related object. Here is the code:
Model:
DocumentLine.cs
/// <summary>
/// States the base implementation for all document lines in a purchasing module.
/// </summary>
public class DocumentLine : Keyed
{
// ... some other properties
/// <summary>
/// Gets or sets the current line's document header.
/// </summary>
[Navigation]
[Display(ResourceType = typeof(Resources.ApplicationResources), Name = "Header")]
public virtual DocumentHeader Header { get; set; }
}
DocumentHeader.cs
/// <summary>
/// States the base implementation for all document headers in a purchasing module.
/// </summary>
public class DocumentHeader : Keyed
{
/// <summary>
/// Gets or sets the current header's document number.
/// </summary>
[Required]
[Display(ResourceType = typeof(Resources.ApplicationResources), Name = "DocumentNumber")]
public string DocumentNumber { get; set; }
/// <summary>
/// Gets or sets the extra cost of the document.
/// </summary>
[Display(ResourceType = typeof(Resources.ApplicationResources), Name = "ExtraCost")]
[RegularExpression(@"^\d*$", ErrorMessageResourceType=typeof(Resources.ApplicationResources), ErrorMessageResourceName= "Exception_ExtraCost_Error")]
public decimal ExtraCost { get; set; }
/// <summary>
/// Gets or sets the vat's extra cost of the document.
/// </summary>
[Display(ResourceType = typeof(Resources.ApplicationResources), Name = "ExtraVat")]
[RegularExpression(@"^\d*$", ErrorMessageResourceType = typeof(Resources.ApplicationResources), ErrorMessageResourceName = "Exception_ExtraVat_Error")]
public decimal ExtraVat { get; set; }
/// <summary>
/// Gets or sets the navigation property to all dependant Document lines.
/// </summary>
[Required]
[Navigation]
[Display(ResourceType = typeof(Resources.ApplicationResources), Name = "DocumentLines")]
public virtual ICollection<DocumentLine> DocumentLines { get; set; }
}
View:
@Html.HiddenFor(model => model.Header.Id, Model.Header != null ? Model.Header.Id : null)
<div class="display-label">
@Html.DisplayNameFor(model => model.Header.ExtraCost)
</div>
<div class="display-field">
<input type="text" name="Header.ExtraCost" id="Header.ExtraCost" data-varname="header.extraCost" value="@(Model.Header.ExtraCost)" />
@Html.ValidationMessageFor(model => model.Header.ExtraCost)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.Header.ExtraVat)
</div>
<div class="display-field">
<input type="text" name="Header.ExtraVat" id="Header.ExtraVat" data-varname="header.extraVat" value="@(Model.Header.ExtraVat)" />
@Html.ValidationMessageFor(model => model.Header.ExtraVat)
</div>
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 Header.Id
value. This view correctly shows the values so I do not think the problem is in here.
Controller:
I have a generic controller method to edit which works fine, although I may have to override it for this particular case.
/// <summary>
/// Handles the POST event for the Edit action, updating an existing TEntity object.
/// </summary>
/// <param name="id">Id of the TEntity object to update.</param>
/// <param name="model">TEntity object with properties updated.</param>
/// <returns>Redirection to the Index action if succeeded, the Edit View otherwise.</returns>
[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);
}
}
The main problem is that this code is not only not editing the related object values, but also sets to null the Header.Id
value of the DocumentLine.
Any advice?