2011-11-28 24 views
4

ブロックにifのjavascriptを挿入するにはどうすればよいですか?ASP.NET MVC - Razor and JavaScript

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

私はbad compile constant valueを取得しています。

答えて

5

$符号は、RazorにHtmlモードに切り替えるように通知しません。スクリプトは<text>タグに包まれ

場所:

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