ASP.net MVC - 显示模板集合 [英] ASP.net MVC - Display Template for a collection
问题描述
我在MVC以下型号:
public class ParentModel
{
public string Property1 { get; set; }
public string Property2 { get; set; }
public IEnumerable<ChildModel> Children { get; set; }
}
当我想显示所有孩子的父模式,我可以这样做:
When I want to display all of the children for the parent model I can do:
@Html.DisplayFor(m => m.Children)
我可以再创建一个ChildModel.cshtml显示模板和DisplayFor会自动遍历列表。
I can then create a ChildModel.cshtml display template and the DisplayFor will automatically iterate over the list.
如果我想创建一个IEnumerable的自定义模板是什么?
What if I want to create a custom template for IEnumerable?
@model IEnumerable<ChildModel>
<table>
<tr>
<th>Property 1</th>
<th>Property 2</th>
</tr>
...
</table>
我怎样才能创建具有的IEnumerable℃的型号类型显示模板; ChildModel&GT;
,然后调用 @ Html.DisplayFor(M =&GT ; m.Children)
没有它抱怨机型是错误的。
How can I create a Display Template that has a model type of IEnumerable<ChildModel>
and then call @Html.DisplayFor(m => m.Children)
without it complaining about the model type being wrong?
推荐答案
这样的:
@Html.DisplayFor(m => m.Children, "YourTemplateName")
或者是这样的:
[UIHint("YourTemplateName")]
public IEnumerable<ChildModel> Children { get; set; }
很明显,您将有〜/查看/共享/ DisplayTemplates / YourTemplateName.cshtml
:
@model IEnumerable<ChildModel>
<table>
<tr>
<th>Property 1</th>
<th>Property 2</th>
</tr>
...
</table>
这篇关于ASP.net MVC - 显示模板集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!