4

JavaScript 関数内で razor 変数を評価する必要があります。次のコードがあるとします。

 @{var unseenNotificationsExist = false;}
 
 @foreach(var notification in viewModel)
 {
    if (notification.WasSeen == false)
    {
      unseenNotificationsExist = true;
      break;
    }
 }

unseenNotificationsExist次のように JavaScript で変数を評価する必要があります。

 if(@unseenNotificationsExist == true)
        $('#unseenNotifCount').addClass('notifNotSeen');

ただし、JavaScript コードをデバッグすると、次のようになります。

if(True == true)

また、次のエラーが表示されます。

Uncaught ReferenceError True が定義されていません

4

6 に答える 6

5
var unseenNotificationsExist = @(unseenNotificationsExist?"true":"false") ;
//the line above must be part of your view

if(unseenNotificationsExist === true)
        $('#unseenNotifCount').addClass('notifNotSeen');
//this can be part of your js file or alternatively and less 
//preferably part of your view
于 2013-08-08T08:44:35.697 に答える
4

回避策を見つけました:これを追加しました

var True = true

現在、javascript には変数 "True" が定義されており、評価は True == true になり、true を返します。

于 2013-08-08T08:50:44.403 に答える
0

JavaScript をレンダリングしたくない場合は、Daniele の提案を使用してください。

JavaScript で比較を行いたい場合:

if(@unseenNotificationsExist.ToString().ToLower() === true)
于 2013-08-08T08:45:15.280 に答える
-1

使ってみて

if('@unseenNotificationsExist')
于 2013-08-08T08:42:26.683 に答える
-1

あなたが試した:

if('@unseenNotificationsExist' === 'True')
        $('#unseenNotifCount').addClass('notifNotSeen');
于 2013-08-08T08:42:32.657 に答える