几个子类(例如,Cheese)共享从基类(Product)派生的公共属性,其属性如SKU,Name和Description.
为了避免在渲染显示/编辑器模板时出现重复,我希望每个子类模板(Cheese.cshtml)在其共享公共基类模板(Product.cshtml)下面呈现其唯一字段.
但是,从派生类转换为基类(Product)cheese并尝试在子类模板中显示其模板没有任何效果.
DisplayTemplate文件结构:
.\Views\Shared\DisplayTemplates
.\Product.cshtml -- renders common base class fields
.\Cheese.cshtml -- renders unique class fields and calls Product.cshtml
Cheese.chtml:
@model Application.Data.Models.Cheese
@{
var product = (Application.Data.Models.Part)Model;
}
Base Product Fields:
@Html.DisplayFor(x => products,"Product") @* no effect! *@
转换到基类并呈现Product.cshtml模板可以从View中正常工作,但不能在子类模板中工作.
如何从子类模板中为基类渲染公共模板?