4

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

6 Answers 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
Sign up to request clarification or add additional context in comments.

1 Comment

It also works including all the code in the javascript snippet in the view
4

I found a workaround: I added this

var True = true

Now, in javascript there is a variable "True" defined and the evaluation comes down to True == true, which returns true.

Comments

2

I prefer to use Json.Encode , whenever i want to convert the model data to JSON objects or JSON variables .

For converting an object(dto) of model to javascript

var accountDto = @Html.Raw(Json.Encode(@Model.AccountDto))

For converting a (bool)property of model

var isTrue = @Json.Encode(Model.IsTrue)

Comments

0

If you want to not have the the JavaScript rendered, use Daniele's suggestion.

If you insist on making the comparison in JavaScript:

if(@unseenNotificationsExist.ToString().ToLower() === true)

5 Comments

Though this might work but it will be a good practice to change the varible into javascript our self and then manipulate it. Just depending upon ToString() wont be a good option
also this type of patten wont allow the user to separate all the js code in a separate file
It's not a pretty solution, agreed! But your solution will not allow a separate JS file either... ;-) EDIT: In fact, I take that back - you were certainly only referring to the if part.
why not.. i just making a global variable.. and i have edited my ans please check again
Yeah I see that now, it wasn't there when I posted, but I figured that out eventually.
-1

You tried with:

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

4 Comments

it still will remain True and not true
Yes, I did, same result.
It's only missing an @ in front of the if imo, possibly curly braces too to make it a C#-if.
the if is in javascript not razor
-1

try using

if('@unseenNotificationsExist')

3 Comments

My answer was incomplete, I edited it. This seems to work for me.
this will always go into true block because in js dosent matter you check if("True") or if("False")
correct. you could not initialize the variable with false though, just declare it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.