2013-03-14 67 views
1

我有剃须刀语法嵌套和混合在HTML和JavaScript。什么是嵌套和混合代码块的正确剃刀语法?

它运行良好,除非我做自动代码格式,}不断推出。

@section js{ 
    <script type="text/javascript"> 
     $(document).ready(function() { 

      @if (ViewBag.Searched && ViewBag.Found) 
      { 
       @:$('#found').show(); $('#permit-info').show(); 

       @if (ViewBag.PendingRequest) 
       { 
        @:$('#request-info').hide(); $('#report-info').show(); 
       } 
       else 
       { 
        @:$('#request-info').show(); $('#report-info').hide(); 
       } 
      } 
      else 
      { 
       @if (ViewBag.Searched) 
       { 
        @:$('#not-found').show(); 
          } // <----- PROBLEM. this } doesn't stay at the right place. 
      } 
     }); 
    </script> 
} 

回答

3

你必须在这条线的额外@

@if (ViewBag.Searched) 

这行:

@if (ViewBag.PendingRequest) 

因为你在剃刀代码是已经(由父母支配if/else语句)。

其实,它仍然杀死格式。 虽然,如果您尝试使用<text>标签而不是@:它的作品。试试这个:

$(document).ready(function() { 

     @if (ViewBag.Searched && ViewBag.Found) 
     { 
      <text>$('#found').show(); $('#permit-info').show();</text> 

      if (ViewBag.PendingRequest) 
      { 
       <text>$('#request-info').hide(); $('#report-info').show();</text> 
      } 
      else 
      { 
       <text>$('#request-info').show(); $('#report-info').hide();</text> 
      } 
     } 
     else 
     { 
      if (ViewBag.Searched) 
      { 
       <text>$('#not-found').show();</text> 
      } 
     } 
    }); 

请注意:Visual Studio中总是缩进剃刀看法不佳:)

+0

我删除了,但还是有同样的问题。 – 2013-03-14 15:52:01

+0

@RayCheng我刚刚编辑过,你实际上有两行'@' – mattytommo 2013-03-14 15:52:20

+0

是的,我试了两个。 – 2013-03-14 15:52:41