嵌套 @Html.DisplayFor(model => baseClass, "BaseClass") 用于基类模板不呈现 [英] Nested @Html.DisplayFor(model => baseClass, "BaseClass") for base class template not rendering

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

问题描述

几个子类(例如 Cheese)共享从基类(Product)派生的公共属性,具有 SKU 等属性>名称描述.

为了避免在呈现显示/编辑器模板时重复,我希望每个子类模板 (Cheese.cshtml) 在其共享的公共基类模板 (Product.cshtml).

但是,从派生类转换到基类(Product)cheese并尝试在子类模板中显示其模板是无效的.

DisplayTemplate 文件结构:

.ViewsSharedDisplayTemplates.Product.cshtml -- 呈现公共基类字段.Cheese.cshtml -- 呈现唯一的类字段并调用 Product.cshtml

Cheese.chtml:

@model Application.Data.Models.Cheese@{var product = (Application.Data.Models.Part)Model;}基础产品领域:@Html.DisplayFor(x => products, "Product") @* 没有效果!*@<div class="field"><div class="display-label">@Html.LabelFor(m => Model.UniqueProperty)</div><div class="display-field">@Html.DisplayFor(m => Model.UniqueProperty)</div>

转换到基类并呈现 Product.cshtml 模板在视图中工作正常,但不能在子类模板中工作.

如何从我的子类模板中为我的基类呈现通用模板?

解决方案

Solution Found

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

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

或者,如果您的文件结构允许,更简洁的路径:

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

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

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.

DisplayTemplate File structure:

.ViewsSharedDisplayTemplates
    .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>

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?

解决方案

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(model => baseClass, "BaseClass") 用于基类模板不呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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