【问题标题】:MVC View change specified row bgcolorMVC视图更改指定行bgcolor
【发布时间】:2015-06-10 02:03:42
【问题描述】:

在我的 MVC 视图中,我在表格中显示来自我的模型的数据列表,如下所示。当我的 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>

【问题讨论】:

    标签: asp.net-mvc if-statement html-table asp.net-mvc-views


    【解决方案1】:

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

    <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 一样

    【讨论】:

      猜你喜欢
      • 2010-12-31
      • 2012-06-26
      • 2017-06-24
      • 2017-12-07
      • 1970-01-01
      • 2022-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多