@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", "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>
    }
}

关于c# - 如何确定模型在Razor View中是否为空,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6038197/

10-11 03:33
查看更多