简体   繁体   English

Javascript if语句将不执行MVC3 / razor

[英]Javascript if statement will not execute MVC3/razor

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: 在Fiddler中,我可以看到我的if语句在浏览器中呈现为:

if ('True' == 'True')

yet for some reason the alert('Blah blah blah True') will not execute. 但由于某种原因,警报('Blah blah blah True')将不会执行。 If I step through the code in VS I can watch it get as far as the if statement then just bug out. 如果我逐步浏览VS中的代码,我可以看到它达到了if语句的程度,然后就可以进行调试。 Any thoughts? 有什么想法吗?

Your going to need to transition from code to html like so. 您将需要像这样从代码过渡到html。 Otherwise the compiler will assume that alert is ac# method call (which it isn't) and you'll get a compiler error when you attempt to use it. 否则,编译器将假定alert是ac#方法调用(不是),并且在尝试使用它时会出现编译器错误。

<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. 另外,如果您的JavaScript占用多行<text></text>则可以使用psuedo元素<text></text> The @: is only for the text following all the way until a newline. @:仅用于文本,一直到换行符为止。

Did you try: 你试过了吗:

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

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.. 编辑:通过将其回滚到三进制来修复代码。

Try === instead maybe? 试试===代替吗?

Never used this MVC3/razor stuff ( or even know what it is ), so this is just a guess 从未使用过这种MVC3 /剃须刀的东西(甚至不知道它是什么),所以这只是一个猜测

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM