在Razor

时间:2018-01-06 18:32:40

标签: c# asp.net asp.net-mvc razor web

我有这个简单的剃​​刀页面:

@model IEnumerable<Gestor.Models.PrecoExportacao>
<table class="table">
<tr>
    <th>
        @Html.DisplayNameFor(model => model.LinhaUn)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Descricao)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Apelido)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.PesoLiquido)
    </th>
...
</tr>

如果model.PesoLiquido的值为0,我希望它不显示,但保留表中的空格。我该怎么办?

3 个答案:

答案 0 :(得分:2)

数据的完整显示表的代码可能如下所示:

site-wrapper

答案 1 :(得分:0)

只需使用if声明;

<th>
    @if(model.PesoLiquido > 0)
    {
        @Html.DisplayNameFor(model => model.PesoLiquido)
    }
</th>

另一种选择是;

<th>
    @Html.DisplayNameFor(model => model.PesoLiquido > 0 ? model.PesoLiquido : "")
</th>

答案 2 :(得分:-1)

您可以将Model放入foreach循环并使用if语句检查列表中的每个模型。以下是:

@model IEnumerable<Gestor.Models.PrecoExportacao>
    <table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.LinhaUn)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Descricao)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Apelido)
        </th>
        foreach(var model in Model)
        {
            @if(model.PesoLiquido != 0)
            {
                <th>
                    @Html.DisplayNameFor(model => model.PesoLiquido)
                </th>
            }
        }
    ...
    </tr>

Razor视图允许您在将服务器发送到客户端之前使用C#语言逻辑来管理服务器上的视图。这意味着您可以使用以下语句:ifforwhiletryvar foo = 420 ......