Evaluate Razor variable in javascript
asp.net-mvc, javascript, razor
Solution
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
Problem
I need to evaluate a razor variable inside a JavaScript function. Given the following code: ``` @{var unseenNotificationsExist = false;} @foreach(var notification in viewModel) { if (notification.WasSeen == false) { unseenNotificationsExist = true; break; } } ``` I need to evaluate the `unseenNotificationsExist` variable in JavaScript like this: ``` if(@unseenNotificationsExist == true) $('#unseenNotifCount').addClass('notifNotSeen'); ``` However, when I debug the JavaScript code, I get this: ``` if(True == true) ``` and also I get the following error: Uncaught ReferenceError True is not defined