嵌套@ Html.DisplayFor(型号=>基类," BaseClass的")为基类模板未呈现 [英] Nested @Html.DisplayFor(model => baseClass, "BaseClass") for base class template not rendering

查看:252
本文介绍了嵌套@ Html.DisplayFor(型号=>基类," BaseClass的")为基类模板未呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

若干子类(如:奶酪的)从基类派生共享公共属性(产品的)像性质的 SKU 名称说明

Several sub-classes (e.g. Cheese) share common properties derived from a base class (Product) with properties like SKU, Name and Description.

要避免重复渲染显示/编辑模板时,我想每个子类模板( Cheese.cshtml 的)来渲染低于其共同的基类模板其独特的领域(的 Product.cshtml 的)。

To avoid duplication when rendering display/editor templates, I want each sub-class template (Cheese.cshtml) to render its unique fields below that of its shared common base class template (Product.cshtml).

不过,从派生类转换为基类(产品)奶酪并试图显示其子类模板内模板有没有效果。

However, casting from the derived class to the base class (Product)cheese and trying to display its template inside the sub-class template has no effect.

.\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! *@

<div class="field">
    <div class="display-label">@Html.LabelFor(m => Model.UniqueProperty)</div>
    <div class="display-field">@Html.DisplayFor(m => Model.UniqueProperty)</div>
</div>

铸造基类和渲染的 Product.cshtml 的模板工作正常从视图,而不是从子类模板中。

Casting to the base class and rendering the Product.cshtml template works fine from a View, but not from within the sub-class template.

我如何从我内子类模板渲染我的基类常见的模板?

How can I render a common template for my base class from within my sub-class templates?

推荐答案

@ Html.DisplayFor(...) cannot嵌套,所以你必须使用 @ Html.Partial 在派生的模板,像这样:

Solution Found

@Html.DisplayFor(...) cannot be nested, so you have to use @Html.Partial in your derived template like so:

@Html.Partial("~/Views/Shared/DisplayTemplates/Product.cshtml", product) %>

或者,如果你的文件结构允许的话,一个更简洁的路径:

Or, if your file structure allows it, a more terse path:

@Html.Partial("DisplayTemplates/Product", product)

这篇关于嵌套@ Html.DisplayFor(型号=&GT;基类,&QUOT; BaseClass的&QUOT;)为基类模板未呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆