MVC剃刀显示模板 [英] MVC Razor display template

查看:131
本文介绍了MVC剃刀显示模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我传递到视图的项目清单。我想呈现使用显示模板每个项目。然而,什么是错的,因为我没有得到现场正确呈现。
这里是我的主视图(Index.cshtml):

  @model IEnumerable的< CustomEntity>@ {
    ViewBag.Title =指数;
}
@ Html.DisplayFor(M =>米)

下面是我的显示模板:

  @model CustomEntity
< D​​IV>
    @ Html.LabelFor(M = GT; m.Name):
    <强> @ Model.Name< / STRONG>
    @ Html.LabelFor(M = GT; m.Icon):
    <强> @ Model.Icon< / STRONG>
    @ Html.LabelFor(M = GT; m.TypeName):
    <强> @ Model.TypeName< / STRONG>
< / DIV>

该页面加载,但不显示该实体的值


解决方案

  @model IEnumerable的< CustomEntity>@ {
    ViewBag.Title =指数;
}
@ Html.DisplayForModel()

然后确保您的显示模板存储在〜/查看/共享/ DisplayTemplates / CustomEntity.cshtml 。注意,位置和显示模板的名称是很重要的。它应该被称为以同样的方式作为类型( CustomEntity.cshtml ),并应设无论是在〜/查看/共享/ DisplayTemplates 或者你也可以覆盖它在〜/查看/ XXX / DisplayTemplates ,其中 XXX 是控制器呈现的主视图。

I have a list of items that I am passing to a view. I would like to render each item using a display template. However, something is wrong, as I don't get the field properly rendered. Here is my main view (Index.cshtml):

@model IEnumerable<CustomEntity>

@{
    ViewBag.Title = "Index";
}
@Html.DisplayFor(m=>m) 

Here is my display template:

@model CustomEntity
<div>
    @Html.LabelFor(m=>m.Name):
    <strong>@Model.Name</strong>
    @Html.LabelFor(m=>m.Icon):
    <strong>@Model.Icon</strong>
    @Html.LabelFor(m=>m.TypeName):
    <strong>@Model.TypeName</strong>
</div>

The page loads, but doesn't display the values of the entity.

解决方案

@model IEnumerable<CustomEntity>

@{
    ViewBag.Title = "Index";
}
@Html.DisplayForModel()

and then make sure that your display template is stored in ~/Views/Shared/DisplayTemplates/CustomEntity.cshtml. Notice that the location and the name of the display template is very important. It should be called the same way as the type (CustomEntity.cshtml) and should be located either in ~/Views/Shared/DisplayTemplates or you could also override it in ~/Views/XXX/DisplayTemplates where XXX is the controller that rendered the main view.

这篇关于MVC剃刀显示模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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