c# - 如何从 MVC4 中的 View 编辑父项的属性?

标签 c# asp.net asp.net-mvc asp.net-mvc-4 razor

这些是我的模型的类:

/// <summary>
/// States the base implementation for all document lines in a purchasing module.
/// </summary>
public class DocumentLine : Keyed
{
    /// <summary>
    /// Document number of the document line.
    /// </summary>
    [Display(ResourceType = typeof(Resources.ApplicationResources), Name = "DocumentNumber")]
    public string DocumentNumber { get; set; }

    ...
}

和:

/// <summary>
/// Defines a line of a Delivery Note document.
/// </summary>
[MetadataType(typeof(DeliveryNoteLineMetadata))]
public class DeliveryNoteLine : DocumentLine
{
   ...
    /// <summary>
    /// Internal class for metadata.
    /// </summary>
    internal class DeliveryNoteLineMetadata
    {
        /// <summary>
        /// Adds the RequiredAttribute to the inherited DocumentNumber property.
        /// </summary>
        [Required]
        public string DocumentNumber { get; set; }
    }
}

这里是 Edit.cshtml 的 View 代码的一部分:

<div class="display-label">
    @Html.LabelFor(model => model.DocumentNumber)
</div>
<div class="editor-field">
    @Html.TextBoxFor(model => model.DocumentNumber, new { @placeholder = @ApplicationResources.DocumentNumber })
</div>

这是我的 Controller 的方法

/// <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 对象的属性 DocumentNumber 的值没有改变,我很难理解 MVC4 Controller 在内部是如何工作的。

有什么建议吗?

提前致谢!

最佳答案

首先,让 DeliveryNoteLineMetadata 继承自 DocumentLine:

internal class DeliveryNoteLineMetadata : DocumentLine

然后只需更改 getter 和 setter 以使用基值(同时添加 new 以隐藏 base 属性):

[Required]
public new string DocumentNumber 
{ 
    get 
    { 
        return base.DocumentNumber;
    }
    set
    {
        base.DocumentNumber = value;
    }
}

关于c# - 如何从 MVC4 中的 View 编辑父项的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19114906/

相关文章:

c# - 将 linq 匿名对象传递给函数

javascript - <asp :Button> doesn't fire 'onclick' event with using UseSubmitBhaviour=false

jquery - 使用 Ajax.ActionLink,如何显示自定义模式确认?

.net - 获取 msbuild 以远程部署干净的应用程序

c# - ODP.NET 存储过程和可选参数

C#:在 XML 中存储字节数组

asp.net - IE7 中的 DropDownList BackColor

c# - 在 ASP.net C# 中不使用 GridView 输出数据集的最简单方法是什么

asp.net-mvc - 如果客户端不可用,如何自动重载 DELETE 和 PUT?

C#井字游戏效率