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)

14
  • 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. Commented May 27, 2014 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 Commented May 27, 2014 at 20:54
  • @Tom ... or use != instead of !== Commented May 27, 2014 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. Commented May 27, 2014 at 20:58
  • 1
    I think you're mixing javascript and c# in a way that won't work.. can you post the generated javascript? Commented May 27, 2014 at 21:03

4 Answers 4

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

2 Comments

Neither of these work. The model is coming back as null anyways, not undefined.
I moved my if statement to the razor part of my code, and put the javascript I needed to do in the <text> places you have. It works! Thank you, I just still don't understand why it wouldn't work as javascript
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/

Comments

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

Comments

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>
}

1 Comment

Your second choice is close to what I ended up doing.

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.