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中,难道我们没有更好的直截了当的方法吗