在 Razor 中有条件地显示 <th> 标签
Conditionally Display <th> tag in Razor
我的代码(enclosed in @for loop)
中有多行 <th>
& <td>
标签,我想有条件地显示这些标签。其中一个 <th>
标签如下所示:
@for (int j = 0; j < Model.Children.Count; j++)
{
if (Model.Children[j].isCollapsible == true || Model.Children[j].IsCompany == true)
{
<th class="..." style="width: 180px;">
@Html.LabelForModel(Model.Children[j].Title)
@if (Model.Children[j].isCollapsible == true && Model.Children[j].IsCompany == false)
{
// lines of code
}
</th>
}
}
虽然这样很好用,但是为了让代码更紧凑,我想把上面代码片段中外面的if
换成条件style=display:none
属性对于 <th>
标签
我试过style="width: 180px; display: "@(Model.Children[j].isCollapsible == true || Model.Children[j].IsCompany == true)" ? none : table-cell"
但是它显示错误:
有人可以告诉我我做错了什么吗?
用括号将三元组括起来,每个值都用引号引起来。
display:@(Model.Children[j].isCollapsible || Model.Children[j].IsCompany ? "none" : "table-cell")
这是一个精简版,可以很容易地看到引号的位置。
style="display:@(condition ? "" : "")"
尝试去掉display:
后面的"
,然后在none
和table-cell
外加上""
。
<th class="..." style="width: 180px;display:@(Model.Children[j].isCollapsible==true || Model.Children[j].IsCompany==true ? "none" : "table-cell")">
...
</th>
我的代码(enclosed in @for loop)
中有多行 <th>
& <td>
标签,我想有条件地显示这些标签。其中一个 <th>
标签如下所示:
@for (int j = 0; j < Model.Children.Count; j++)
{
if (Model.Children[j].isCollapsible == true || Model.Children[j].IsCompany == true)
{
<th class="..." style="width: 180px;">
@Html.LabelForModel(Model.Children[j].Title)
@if (Model.Children[j].isCollapsible == true && Model.Children[j].IsCompany == false)
{
// lines of code
}
</th>
}
}
虽然这样很好用,但是为了让代码更紧凑,我想把上面代码片段中外面的if
换成条件style=display:none
属性对于 <th>
标签
我试过style="width: 180px; display: "@(Model.Children[j].isCollapsible == true || Model.Children[j].IsCompany == true)" ? none : table-cell"
但是它显示错误:
有人可以告诉我我做错了什么吗?
用括号将三元组括起来,每个值都用引号引起来。
display:@(Model.Children[j].isCollapsible || Model.Children[j].IsCompany ? "none" : "table-cell")
这是一个精简版,可以很容易地看到引号的位置。
style="display:@(condition ? "" : "")"
尝试去掉display:
后面的"
,然后在none
和table-cell
外加上""
。
<th class="..." style="width: 180px;display:@(Model.Children[j].isCollapsible==true || Model.Children[j].IsCompany==true ? "none" : "table-cell")">
...
</th>