微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 如何在Razor View中确定模型是否为空

@model IEnumerable<Framely2011.Models.Frames>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New","Create")
</p>
<table>
    <tr>
        <th></th>
        <th>
            PictureID
        </th>
        <th>
            UserID
        </th>
    </tr>

@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.ActionLink("Edit","Edit",new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Details","Details",new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Delete","Delete",new { /* id=item.PrimaryKey */ })
        </td>
        <td>
            @item.PictureID
        </td>
        <td>
            @item.UserID
        </td>
        <td>
            Meta 1: @item.MetaTagsObj.Meta1 Meta 2: @item.MetaTagsObj.Meta2 Meta 3: @item.MetaTagsObj.Meta3
        </td>
    </tr>
}

</table>

如果模型出现空白,我怎么才能让它打印出“没有框架”以便根本不打印任何html表格,我认为简单的if语句就足够了,但我是razor的新手我不确定如何做到这一点.

解决方法

将其添加页面顶部:
@using System.Linq

然后用这个块替换你的代码.

@if( !Model.Any() )
{
    <tr><td colspan="4">There are no Frames</td></tr>
}
else
{
    foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.ActionLink("Edit",new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Details",new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Delete",new { /* id=item.PrimaryKey */ })
            </td>
            <td>
                @item.PictureID
            </td>
            <td>
                @item.UserID
            </td>
            <td>
                Meta 1: @item.MetaTagsObj.Meta1 Meta 2: @item.MetaTagsObj.Meta2 Meta 3: @item.MetaTagsObj.Meta3
            </td>
        </tr>
    }
}

原文地址:https://www.jb51.cc/csharp/98847.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐