0

I'm not sure whats wrong with my if statement. I'm trying to use my model inside my javascript.

if (@Model !== null)
{
    if (@Model.Level !== null)
    {
        //use @Model.Level
    }
}
else
{
    //use default
}

The Model is null, but it still steps into the first if statement (and breaks on the second one obviously). I've tried @Model, !@Model and != but it still always steps in.

What am I doing wrong? (It's also got squiggly red lines under both !== saying there is a syntax error)

Bobo
  • 976
  • 2
  • 10
  • 25
  • Try `if(@Model && @Model.Level) { ... }`. I believe you're breaking because `undefined !== null`. I'm betting your values are `undefined` instead of `null`. Try the truthy/falsy syntax above. – War10ck May 27 '14 at 20:54
  • 2
    I'm not too familiar with MVC and if this works the way I think it would, but... what if you did `if(@Model)` instead of comparing it to `null`, that way this picks up `undefined` as well – Tom May 27 '14 at 20:54
  • @Tom ... or use `!=` instead of `!==` – Pointy May 27 '14 at 20:56
  • I've tried both of those. The model is definitely null when I am debugging, so it shouldn't worry about undefined for now. – Bobo May 27 '14 at 20:58
  • And you're sure it's `null` and not the string `"null"`? – CupawnTae May 27 '14 at 21:00
  • Its not the string "null". The actual model is null. – Bobo May 27 '14 at 21:00
  • @Bobo What is the type of the model? – Jason P May 27 '14 at 21:01
  • It's a ViewModel created for my project – Bobo May 27 '14 at 21:02
  • 1
    I think you're mixing javascript and c# in a way that won't work.. can you post the generated javascript? – Jason P May 27 '14 at 21:03
  • On second glance, since the if statements are (or at least look like) javascript, `@Model.Level` will be accessed regardless of whether `@Model` is null, and should throw a `NullReferenceException` if it is. – Jason P May 27 '14 at 21:10
  • The generated looks just the same – Bobo May 27 '14 at 21:11
  • Can't be the same - `@Model` is not valid javascript. Not familiar enough with MVC to know what `@Model` will be replaced with if Model is null, but what @JasonP says makes sense - you probably want to move your if/null check into the MVC domain and out of javascript – CupawnTae May 27 '14 at 21:13
  • @Bobo Is the code you've shown us located in a .js file or a .cshtml file? – Jason P May 27 '14 at 21:14
  • This is in a .cshtml file. I fixed it by moving this into a razor if statement, which is working. I'm still not sure why the javascript doesn't work though – Bobo May 27 '14 at 21:18

4 Answers4

2

Triple equations work without type castings in JavaScript. In your case you are might get an undefined object which isn't a null value.

For example:

undefined === null //Do not cast when comparing, increased performance.
false
undefined == null //Do cast when comparing, decreased performance.
true

In addition, if @Model value is null then you won't see a null value on client side. It gives you an empty value like this:

if( == null)
{
}

This will cause an error on your javascript side. Null check should be done at server side. For that reason you have to put @ value in front of your code to make it server side:

<script>
    @if (Model != null) //Server side code.
        { 
            if (Model.Level != null) //still server side code.
            {

                 <text>
                   alert("your javascript here"); //write javascript on your screen.
                 </text>  
            }
        }
</script>
Bura Chuhadar
  • 3,653
  • 1
  • 14
  • 17
  • Neither of these work. The model is coming back as null anyways, not undefined. – Bobo May 27 '14 at 21:01
  • I moved my if statement to the razor part of my code, and put the javascript I needed to do in the places you have. It works! Thank you, I just still don't understand why it wouldn't work as javascript – Bobo May 27 '14 at 21:20
0

In order to check if something is null or undefined in javascript, use if (@model) rather than if ( @model !== null) http://appendto.com/2010/10/how-good-c-habits-can-encourage-bad-javascript-habits-part-2/

genisage
  • 1,159
  • 7
  • 17
0

The reason why it steps into the if statement is because it evaluates to true, no weirdness to be found here. Your browser is not temperamental. For a list comparisons check out this http://dorey.github.io/JavaScript-Equality-Table/

Also note that there is a difference between double and triple =. Triple will type cast

TBAR
  • 448
  • 2
  • 10
0

This code looks a LOT like a Razor then Javascript, though you may be trying to mix the two of them together.

Your choices:

1) Convert Model to a JavaScript object using something like this:
Turn C# object into a JSON string in .NET 4

2) Use the Razor if statement and write out your final JavaScript with it.

<script>
  // Code assume this is an numeric value
  var useThisVariable;
</script>

if (@Model !== null)
{
    if (@Model.Level !== null)
    {
    <script>
      useThisVariable = @Model.Level;
    </script>

    }
}
else
{
    <script>
      useThisVariable = -1;
    </script>
}
Community
  • 1
  • 1
Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74