I have a ViewBag that has no value. I am not even defining the ViewBag.SelectVale
in my controller so I would expect it to be a null value.
When I do the following in JQuery but does not work:
if ('@ViewBag.SelectVale' == null){
// do something
}
What works instead is something like:
if ('@ViewBag.SelectVale' == ""){
// do something
}
In the .NET framework
nullgets output as an empty string, so if you look at the actual javascript being sent to the browser, in the first case it will say:… and in the second case it will say:
Most javascript programmers would tell you to just change your statement to:
… because empty strings evaluate to
falsewhen they are treated as boolean values in javascript.Or, since there doesn’t appear to be anything really dynamic happening here, try this:
… which will avoid even outputting the “do something” javascript since you know when you are rendering the page that SelectVale is null.