在 JavaScript 中评估 Razor 变量

Oct*_*ure 4 javascript asp.net-mvc razor

我需要评估 JavaScript 函数中的 razor 变量。鉴于以下代码:

 @{var unseenNotificationsExist = false;}
 
 @foreach(var notification in viewModel)
 {
    if (notification.WasSeen == false)
    {
      unseenNotificationsExist = true;
      break;
    }
 }
Run Code Online (Sandbox Code Playgroud)

我需要unseenNotificationsExist像这样在 JavaScript 中评估变量:

 if(@unseenNotificationsExist == true)
        $('#unseenNotifCount').addClass('notifNotSeen');
Run Code Online (Sandbox Code Playgroud)

但是,当我调试 JavaScript 代码时,我得到以下信息:

if(True == true)
Run Code Online (Sandbox Code Playgroud)

而且我收到以下错误:

未捕获的 ReferenceError True 未定义

Par*_*rma 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
Run Code Online (Sandbox Code Playgroud)