加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读