我在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),而不会抱怨模型类型是错误的?
解决方法
喜欢这个:
@Html.DisplayFor(m => m.Children,"YourTemplateName")
或像这样:
[UIHint("YourTemplateName")] public IEnumerable<ChildModel> Children { get; set; }
显然你会有〜/ Views / Shared / DisplayTemplates / YourTemplateName.cshtml:
@model IEnumerable<ChildModel> <table> <tr> <th>Property 1</th> <th>Property 2</th> </tr> ... </table>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。