Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a Javascript variable from my View using a ViewBag property

I would like to set a JavaScript variable in my view (inside a script block) based on the existences of a ViewBag property from the controller... like:

var doAboutTab = @(ViewBag.DoAboutTab != null)

This generates a JavaScript error of:

var doAboutTab = False

'False is not defined'

like image 658
Pablo Avatar asked Feb 23 '23 02:02

Pablo


2 Answers

@if(ViewBag.DoAboutTab != null)
{
    var doAboutTab ="something";
}else
{
    var doAboutTab ="something_else";
}

or

var doAboutTab = '@ViewBag.DoAboutTab' !== '';
like image 160
Praveen Prasad Avatar answered Jun 03 '23 08:06

Praveen Prasad


The false keyword is lower case in javascript.

var doAboutTab = @((ViewBag.DoAboutTab != null).ToString().ToLower());
like image 44
jrummell Avatar answered Jun 03 '23 07:06

jrummell