我有一个在 View 中使用 Razor 的 ASP.NET MVC 应用程序。在我看来,我有以下几点:
<table style="width:100%;">
...
</table>
我想说,
if (items.Count == 0)
,那么表格的背景颜色应该是橙色。然而,if (items.Count > 0)
,我希望表格的背景颜色为灰色。如何使用 Razor 执行此操作?谢谢!
最佳答案
CSS:
.table {
background-color: grey;
}
.table.empty {
background-color: orange;
}
Razor :
<table class="table @(items.Count == 0 ? "empty" : null)">
这将呈现为
<table class="table">
或 <table class="table empty">
.一般来说,这些是核心值(
null
、 ""
、 true
、 false
)如何在 Razor 中的 HTML 属性中呈现的小规则:Razor Rendered HTML
----------------------------------------
<abc xyz="s @(null) t"> <abcxyz="s t">
<abc xyz="@(null)"> <abc>
<abc xyz="@("")"> <abc xyz="">
<abc xyz="@(true)"> <abc xyz="xyz">
<abc xyz="@(false)"> <abc>
注意
null
之间的区别和 ""
(空字符串)。正如 Chris Pratt 在他的评论中指出的那样,如果您不需要支持 IE 9 之前的版本,您可以使用
:empty
伪类在没有三元运算符的情况下获得类似的效果。CSS:
.table {
background-color: grey;
}
.table:empty {
background-color: orange;
}
Razor :
<table class="table">
关于asp.net-mvc - 使用 ASP.NET MVC 的条件格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26660587/