How to get bool value from ViewBag on view?

Olegs Jasjko picture Olegs Jasjko · Aug 19, 2015 · Viewed 28.6k times · Source

I am trying to receive bool values from ViewBag.

But the issue is that to receive value I am using:

var test = '@ViewBag.Test'

In this way value will be 'True' or 'False' (string). Of cource I can use something like if (test == 'True') but, is there a way to get from ViewBag bool value dirrectly, or at least convert it?

EDIT:

I need to get ViewBag value in javascript part.

Answer

J Santosh picture J Santosh · Aug 19, 2015

In controller code is ViewBag.Test = true;

In View code is

var test=@(ViewBag.Test.ToString().ToLower());
console.log(test);

Now the data type of variable test is a bool. So you can use:

if(test)
    alert('true');
else 
    alert('false')