我在编辑相关对象的属性时遇到了一些问题。这是代码:
模型:
文档行.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; }
}
文件头文件
/// <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; }
}
看法:
@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>
我知道 MVC 跟踪输入的 id 和名称以将值传递给控制器,这就是为什么我将隐藏输入作为Header.Id
值。这个视图正确地显示了这些值,所以我认为问题不在这里。
控制器:
我有一个通用的控制器方法来编辑它工作正常,尽管我可能必须针对这种特殊情况重写它。
/// <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);
}
}
主要问题是这段代码不仅没有编辑相关对象的值,而且还把DocumentLine的值设置为空。Header.Id
有什么建议吗?