在我的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>
答案 0 :(得分:2)
您只需要在输出行的位置进行操作。一种方法是:
<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一样