2012-05-04 27 views
4

這裏就是我想要做的事:MVC3剃刀不承認時,停止解析

$(document).ready(function() { 
    @if (ViewBag.VerifyIfLoggedIn) { 

     $("#needlogin-popup").dialog({ 
      modal: true, 
      closeOnEscape: true, 
      minHeight: 384, 
      minWidth: 596, 
      resizable: false, 
      show: { 
       effect: 'slide', 
       duration: 500, 
       direction: 'up' 
      }, 
      hide: { 
       effect: 'slide', 
       duration: 250, 
       direction: 'up' 
      }, 
      title: 'Inicie Sesion' 
     }); 
    } 
}); 

如果事情是真的,那麼這個輸出Javascript代碼的網頁。

但我得到的錯誤:

CS1056: Unexpected character '$'

我怎麼能告訴剃刀停止解析和輸出無論是在條件語句?

回答

10

圍繞代碼使用<text>...</text>

瞭解它是如何在這裏工作:

http://weblogs.asp.net/scottgu/archive/2010/12/15/asp-net-mvc-3-razor-s-and-lt-text-gt-syntax.aspx

The tag is an element that is treated specially by Razor. It causes Razor to interpret the inner contents of the block as content, and to not render the containing tag element (meaning only the inner contents of the element will be rendered – the tag itself will not).

This makes it convenient when you want to render multi-line content blocks that are not wrapped by an HTML element.

$(document).ready(function() { 
    @if (ViewBag.VerifyIfLoggedIn) { 
     <text>$("#needlogin-popup").dialog({ 
      modal: true, 
      closeOnEscape: true, 
      minHeight: 384, 
      minWidth: 596, 
      resizable: false, 
      show: { 
       effect: 'slide', 
       duration: 500, 
       direction: 'up' 
      }, 
      hide: { 
       effect: 'slide', 
       duration: 250, 
       direction: 'up' 
      }, 
      title: 'Inicie Sesion' 
     }); </text> 
    } 
}); 
+0

感謝您解決這個問題對我來說。奇怪,但它解決了它。 –

+0

@SergioTapia - 爲什麼它很奇怪?這就是標籤存在的原因。 –

+0

與非常簡潔的'@'關鍵字相比,它看起來笨重。 –