【发布时间】:2011-12-21 13:41:49
【问题描述】:
我在 MVC 中有以下模型:
public class ParentModel
{
public string Property1 { get; set; }
public string Property2 { get; set; }
public IEnumerable<ChildModel> Children { get; set; }
}
当我想显示父模型的所有子模型时,我可以这样做:
@Html.DisplayFor(m => m.Children)
然后我可以创建一个 ChildModel.cshtml 显示模板,DisplayFor 将自动遍历列表。
如果我想为 IEnumerable 创建自定义模板怎么办?
@model IEnumerable<ChildModel>
<table>
<tr>
<th>Property 1</th>
<th>Property 2</th>
</tr>
...
</table>
如何创建模型类型为 IEnumerable<ChildModel> 的显示模板,然后调用 @Html.DisplayFor(m => m.Children) 而不会抱怨模型类型错误?
【问题讨论】:
标签: asp.net-mvc asp.net-mvc-3 display-templates