【ASP.NET】DisplayForModel的用法

 

在View中显示一个列表信息,我们能这样写,用一个foreach循环展示信息

@model IEnumerable<Wang.OA.Model.UserInfo>

@{
    ViewBag.Title = "Index2";
}

<h2>Index2</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ShowName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Remark)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ModfiedOn)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.SubTime)
        </th>
        <th></th>
    </tr>
    @foreach (var item in Model)
    {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.ShowName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Remark)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.ModfiedOn)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.SubTime)
                </td>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
                    @Html.ActionLink("Details", "Details", new { id = item.Id }) |
                    @Html.ActionLink("Delete", "Delete", new { id = item.Id })
                </td>
            </tr>
    }
</table>

 

我们也可以同DisplayForModel

首先在对于的视图文件夹新建一个DisplayTemplates文件夹,在以Model为名新建一个UserInfo.cshtml文件

在UserInfo.cshtml文件中

@model Wang.OA.Model.UserInfo
<tr>
    <td>
        @Model.Name
    </td>
    <td>
        @Model.ShowName
    </td>
    <td>
        @Model.Remark
    </td>
    <td>
        @Model.ModfiedOn
    </td>
    <td>
        @Model.SubTime
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id = Model.Id }) |
        @Html.ActionLink("Details", "Details", new { id = Model.Id }) |
        @Html.ActionLink("Delete", "Delete", new { id = Model.Id })
    </td>
</tr>

然后在列表视图中,去掉foreach循环,添加    @Html.DisplayForModel()

@model IEnumerable<Wang.OA.Model.UserInfo>

@{
    ViewBag.Title = "Index2";
}

<h2>Index2</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ShowName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Remark)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ModfiedOn)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.SubTime)
        </th>
        <th></th>
    </tr>
    @Html.DisplayForModel()
</table>

 

能实现同样的效果

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GreAmbWang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值