【问题标题】:Why style is not recognized by td element in razor?为什么剃须刀中的 td 元素无法识别样式?
【发布时间】:2019-09-07 06:50:55
【问题描述】:
我在 Razor 代码的 HTML 表中有此列。我想根据状态值改变它的背景。
<td @(@InspectionReport.Status == 0 ? style = "Background-Color: lightgreen;" : @InspectionReport.Status == 1 ? style = "Background-Color: lightgray;" : @InspectionReport.Status == 2 ? style = "Background-Color: blue;" : @InspectionReport.Status == 3 ? style = "Background-Color: yellow;" : "")>
但它说
样式在当前上下文中不存在
【问题讨论】:
标签:
html
asp.net-mvc
razor
model-view-controller
【解决方案1】:
不要直接使用style,而是使用td元素中的某个类
<td class="@(InspectionReport.Status == 0 ? "lightgreen" : InspectionReport.Status == 1 ? "lightgray" :InspectionReport.Status == 2 ? "blue" : InspectionReport.Status == 3 ? "yellow" : "")"></td>
然后在您的 css 文件中创建类,如下所示:
td.lightgreen{
background-color:lightgreen;
}
td.lightgray{
background-color:lightgreen;
}
td.blue{
background-color:blue;
}
td.yellow{
background-color:yellow;
}