Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.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# 对型号'使用EditorFor;s基类_C#_Asp.net_Asp.net Mvc 4_Razor - Fatal编程技术网

C# 对型号'使用EditorFor;s基类

C# 对型号'使用EditorFor;s基类,c#,asp.net,asp.net-mvc-4,razor,C#,Asp.net,Asp.net Mvc 4,Razor,在我的应用程序中,基类/派生类的结构比较复杂,但为了让它更简单,让我们假设一个简单的模型示例: public class Base { public string BaseProp { get; set; } } public class Derived : Base { public string DerivedProp { get; set; } } 我想为这些类制作EditorTemplates,Base.cshtml很简单: @model Base @Html.Edit

在我的应用程序中,基类/派生类的结构比较复杂,但为了让它更简单,让我们假设一个简单的模型示例:

public class Base
{
   public string BaseProp { get; set; }
}

public class Derived : Base
{
   public string DerivedProp { get; set; }
}
我想为这些类制作EditorTemplates,
Base.cshtml
很简单:

@model Base

@Html.EditorFor(model => model.BaseProp)
但是在
Derived.cshtml
中,我想我可以在基类上调用EditorFor,如下所示:

@model Derived

@Html.EditorFor(model => model.DerivedProp)

//This throws an exception in a browser
@Html.EditorFor(model => (Base)model)

//This is without an exception, 
//but nothing happens, there are no fields of base class in the page
@{
    var baseObj = (Base)Model;
    @Html.EditorFor(_ => baseObj)
}
有没有办法,如何正确调用基类的EditorTemplate?我发现了一个只有6年历史的主题,但它提供了一个解决方案,使用自定义助手中的Partial代替EditorFor,这仍然不是一个理想的解决方案。在较新版本的ASP中,难道我们没有更好的直截了当的方法吗