0

I have the following code:

<script type="text/javascript">
    if ('@(Model.DidPass)' == 'True') {
        alert('Blah Blah True');
    }
    else {
        alert('Blah blah false');
    }
</script>

in Fiddler I can see that my if statements renders in the browser as:

if ('True' == 'True')

yet for some reason the alert('Blah blah blah True') will not execute. If I step through the code in VS I can watch it get as far as the if statement then just bug out. Any thoughts?

4
  • maybe a video of me stepping through my code? I promise. It hits the if statement, but doesn't run either alerts. Commented Oct 13, 2011 at 20:11
  • 1
    A screen shot would be awesome, yes Commented Oct 13, 2011 at 20:11
  • I think my problem goes way deeper than this. Because it doesn't make sense that it wouldn't work. I think the partial view I'm returning that this code is in is somehow not getting rendered. Commented Oct 14, 2011 at 13:33
  • Ya, my view wasn't getting rendered properly. The above code works. Commented Oct 14, 2011 at 14:08

4 Answers 4

7

Your going to need to transition from code to html like so. Otherwise the compiler will assume that alert is a c# method call (which it isn't) and you'll get a compiler error when you attempt to use it.

<script type="text/javascript">
    @if(Model.DidPass){
        @:alert('Blah Blah True');
    } else {
        @:alert('Blah blah false');
    }
</script>

Alternatively you can use the psuedo element <text></text> if your javascript takes up more than one line. The @: is only for the text following all the way until a newline.

Sign up to request clarification or add additional context in comments.

Comments

2

Did you try:

    if (Boolean('@(Model.DidPass)') == true) {
        alert('Blah Blah True');
    }
    else {
        alert('Blah blah false');
    }

3 Comments

I down voted because of the unnecessary complexity, awkward syntax, etc.
1st it's not awkward, 2nd it's valid JavaScript, and 3rd it's only for testing purposes. Hence 'Did you try'. Try as in Make an attempt or effort to do something.
1, It looks awkward to me but I do regret downvoting for it. 2, I understand that it's valid javascript, and my dislike of it is largely in part to having dynamic javascript, not your code (sorry). 3, understood.. again, I wish I could roll back a downvote.
1

Eh.. that's not how i'd do it but, since you are going to do it that way why don't you just do:

<script type="text/javascript">
   alert('@(Model.DidPass ? "blahblahblah true" : "blahlbahlbah false")');
</script>

edit : fixed code by rolling it back to a ternary..

6 Comments

I had it like that originally, edited it a lot because I was trying to get it to work. It won't hit either of the alerts no matter how I do it.
Then there is a problem with your javascript somewhere else. Use firebug in Firefox to see what kind of error it gives you.
@Chance, your code won't compile. See BuildStarted's answer to understand why or try it yourself. There is no such function in C# called alert.
@Darin geh, I forgot to wrap in quotes. It's been awhile since I touched razor :/
@Chance, what quotes? You should use @: or wrap in <text> node.
|
0

Try === instead maybe?

Never used this MVC3/razor stuff ( or even know what it is ), so this is just a guess

Comments

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.