asp.net-mvc – ASP.net MVC – 显示模板集合
发布时间:2020-12-15 19:41:59 所属栏目:asp.Net 来源:网络整理
导读:我在MVC中有以下模型: public class ParentModel{ public string Property1 { get; set; } public string Property2 { get; set; } public IEnumerableChildModel Children { get; set; }} 当我想显示所有的孩子为父模型我可以做: @Html.DisplayFor(m = m.
我在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> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – LINQ查询,其中boolean值为true或false
- asp.net-mvc-4 – 检查组是否为空SignalR?
- asp.net – 为什么在web.config中保存动态数据是个坏主意?
- 使用xmlhttp的经典ASP页面可以发出JSON请求吗?
- asp.net-web-api – 用于IFormFile的Asp.Net Core swagger帮
- asp.net-core-mvc – 为dotnet核心asp.net运行CRUD脚手架时
- asp.net-mvc – ASP.NET MVC模型验证错误本地化上下文
- 在servicestack中使用asp.net身份验证
- 深蓝词库转换1.5发布
- ASP.NET MVC Controller.Json DateTime序列化与NewtonSoft
推荐文章
站长推荐
- asp.net – 在会话中存储购物车
- 如何为ASP.NET WebAPI 2中使用路由属性的特定控制
- asp.net – 请求在IIS工作进程中存在于RequestAc
- asp.net-mvc – 是否可以在基于路由的MVC4中使用
- asp.net-mvc-4 – 重载Html.LabelFor以添加’后缀
- asp.net-ajax – system.web不包含脚本的定义
- .net – 修改MVC 5中的密码长度
- ASP.NET bin目录中的Oracle oci.dll无法删除/不起
- asp.net-mvc-3 – ASP.NET MVC视图模型不绑定在H
- asp.net – WebResource Hell – 找不到资源
热点阅读