2011-11-28 175 views
4

我怎样才能把if块内的javascript?ASP.NET MVC - 剃刀和JavaScript

@if(TempData["notification-message"] != null) { 
     $('#notification').jnotifyAddMessage({ 
      text: '@TempData["notification-message"]', 
       permanent: false 
          }); 
} 

我得到bad compile constant value

回答

5

$符号不表示剃刀切换到Html模式。

把脚本包在<text>标签:

@if(!string.IsNullOrEmpty((TempData["notification-message"] as string))) { 
    <text> 
     $('#notification').jnotifyAddMessage({ 
      text: '@TempData["notification-message"].ToString(), 
       permanent: false 
          }); 
    </text> 
}