如何退出if语句已经在表中显示一些标签在Razor?

时间:2016-06-23 07:34:54

标签: asp.net-mvc razor

我有一个asp mvc项目,我希望在局部视图中显示列表。 我用桌子显示它,但我不想显示所有标签。考虑以下代码:

@if(bool.parse(option1)&&bool.parse(option2))
{

<th>Country</th>
<th>State</th>
<th>City</th>
}

@if(bool.parse(option1))
{
  <th>Country</th>
  <th>City</th>
}
  @if(bool.parse(option2))
{
   <th>State</th>
}

<th>other columns</th>

我想如果option1和option2为true则没有运行第2和第3个条件并显示其他列。你怎么做

1 个答案:

答案 0 :(得分:1)

像这样使用else if

@if(bool.parse(option1)&&bool.parse(option2))
{
<th>Country</th>
<th>State</th>
<th>City</th>
}
else if(bool.parse(option1))
{
  <th>Country</th>
  <th>City</th>
}
else if(bool.parse(option2))
{
   <th>State</th>
}

<th>other columns</th>

编辑如果你想在没有else的情况下这样做(我不明白为什么),试试这个:

@if(bool.parse(option1))
{
  <th>Country</th>
}
@if(bool.parse(option2))
{
   <th>State</th>
}
@if(bool.parse(option1))
{
  <th>City</th>
}

<th>other columns</th>

无论如何,我更喜欢这种风格,不是因为我不想使用else,而是因为它看起来更干净而没有冗余。