写剃刀的最佳方式(格式问题)

时间:2016-11-15 01:55:09

标签: asp.net-mvc razor

您好我在剃刀中有以下代码:

@{
                foreach (var item in this.Model)
                {
                    if (item.Items == null)
                    {
                        @: <li @{if (item.Active) { <text> class="active" </text>   } }>

                       if (item.SpanClass == null)
                       {
                            @: <a href="@item.Url" @{ if (item.Active) { <text> class="waves-effect active" </text>    } else { <text> class="waves-effect" </text>     } }><i class="@item.Icon"></i><span> @item.Text </span></a>
                       }else
                       {
                            @: <a href="@item.Url" @{ if (item.Active) { <text> class="waves-effect active" </text>    } else { <text> class="waves-effect" </text>     } }><i class="@item.Icon"></i><span> @item.Text <span class="@item.SpanClass">@item.SpanValue</span></span></a>
                                        }

                        }
                        @: </li>
                     }
            }

看起来像这样:

enter image description here

但我去了:

enter image description here

然后代码格式被破坏:

enter image description here

  

有任何线索吗?

1 个答案:

答案 0 :(得分:0)

我会做那样的事情

@{
    foreach (var item in this.Model)
    {
    var activeClass = item.Active ? "active" : "";
    if (item.Items == null)
    {
        <li class="@activeClass">
            @if (item.SpanClass == null)
            {

                <a href="@item.Url" class="waves-effect @activeClass"><i class="@item.Icon"></i><span> @item.Text </span></a>
            }
            else
            {
                <a href="@item.Url" class="waves-effect @activeClass"><i class="@item.Icon"></i><span> @item.Text <span class="@item.SpanClass">@item.SpanValue</span></span></a>
            }
        </li>
      }
   }
}