Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何编辑父项';从MVC4中的视图中删除属性?_C#_Asp.net_Asp.net Mvc_Asp.net Mvc 4_Razor - Fatal编程技术网

C# 如何编辑父项';从MVC4中的视图中删除属性?

C# 如何编辑父项';从MVC4中的视图中删除属性?,c#,asp.net,asp.net-mvc,asp.net-mvc-4,razor,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. /// &l

以下是我的模型的类:

/// <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; }

    ...
}
//
///说明采购模块中所有文档行的基本实现。
/// 
公共类文档行:已设置关键帧
{
/// 
///文档行的文档编号。
/// 
[显示(ResourceType=typeof(Resources.ApplicationResources),Name=“DocumentNumber”)]
公共字符串DocumentNumber{get;set;}
...
}
以及:

//
///定义交货通知单文档的一行。
/// 
[元数据类型(typeof(DeliveryNoteLineMetadata))]
公共类DeliveryNoteLine:DocumentLine
{
...
/// 
///元数据的内部类。
/// 
内部类DeliveryNoteLineMetadata
{
/// 
///将RequiredAttribute添加到继承的DocumentNumber属性。
/// 
[必需]
公共字符串DocumentNumber{get;set;}
}
}
这里是Edit.cshtml视图代码的一部分:

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

@LabelFor(model=>model.DocumentNumber)
@Html.TextBoxFor(model=>model.DocumentNumber,新的{@placeholder=@ApplicationResources.DocumentNumber})
这是我的控制器的方法

/// <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);
    }
}
//
///处理编辑操作的POST事件,更新现有TEntity对象。
/// 
///要更新的TEntity对象的Id。
///属性已更新的TEntity对象。
///重定向到索引操作如果成功,则编辑视图失败。
[HttpPost]
公共虚拟操作结果编辑(字符串id,TEntity模型)
{
var request=new RestSharp.RestRequest(Resource+“?id={id}”,RestSharp.Method.PUT){RequestFormat=RestSharp.DataFormat.Json}
.AddParameter(“id”,id,RestSharp.ParameterType.UrlSegment)
.AddBody(模型);
var response=Client.Execute(请求);
//处理响应错误
负责人(响应);
如果(Errors.Length==0)
返回操作(“索引”);
其他的
{
ViewBag.Errors=错误;
返回视图(模型);
}
}
这是行不通的。DocumentLine对象的属性DocumentNumber的值没有改变,我很难理解MVC4控制器内部是如何工作的

有什么建议吗


提前谢谢

首先,让
DeliveryNoteLineMetadata
继承自
DocumentLine

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


您是否将更改的值发回控制器方法?如果是的话,你的控制器方法看起来像什么?你能在你的输出HTML中检查这个文本框的ID和名称属性吗?MVC映射通过这些名称传递给控制器的实体。如果名称错误,可能是您需要在CSHTML中手动设置。输出的HTML似乎是正确的:您是在编辑单个DeliveryNoteLine还是同时编辑几个DeliveryNoteLine?如果当时有一些正在编辑,则缺少数组索引。还有,控制器中的张力是多少?固定的。一切正常,但有一个输入type=“hidden”和该值,控制器接受该值,而不是编辑器字段中的值。感谢大家。它说“对象”不包含DocumentNumber的定义:S@Kutyel,抱歉,您需要
new
关键字来隐藏基本属性。此外,您还需要
DeliveryNoteLineMetadata
DocumentLine
继承。请参阅我的编辑恐怕此解决方案不起作用:'(当我调试deliveryNote的值时,基本属性(包括DocumentNumber)永远不会更改。>_
internal class DeliveryNoteLineMetadata : DocumentLine
[Required]
public new string DocumentNumber 
{ 
    get 
    { 
        return base.DocumentNumber;
    }
    set
    {
        base.DocumentNumber = value;
    }
}