MVC 视图更改指定行 bgcolor

MVC View change specified row bgcolor

在我的 MVC 视图中,我在 table 中显示模型中的数据列表,如下所示。当其中一个字段满足我的 if-else 语句中的特定条件时,如何使指定的行改变颜色?似乎我无法在我的 if-else 中添加一个 jquery...

我的看法:

    ...

    <table>

    ...      

    @foreach (var item in Model) {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.field1)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.field2)
                </td>
                <td>
                    @if(item.field3== 0){
                        //change this row to grey color
                }
                else {
                        @Html.DisplayFor(modelItem => item.field3)
                }
                </td>

      ....

     </table>

您只需要在输出行的地方进行操作。一种方法是:

<table>

    ...      

    @foreach (var item in Model) {
            <tr style='background-color: @(item.field3 == 0 ? "gray" : "white");'>
                <td>
                    @Html.DisplayFor(modelItem => item.field1)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.field2)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.field3)
                </td>
      }
      ....

     </table>

构造条件的方法有很多种,这取决于你的场景。但关键是您可以在该 foreach 循环中的任何位置访问 item 对象的 field3 属性,就像普通的 C# foreach