开发者

MVC3 Razor and Javascript - lots of syntax errors in green

开发者 https://www.devze.com 2023-03-25 12:27 出处:网络
I am starting to notice problems when I try and code my javascript and use functions that are in my viewmodel. Things like this:

I am starting to notice problems when I try and code my javascript and use functions that are in my viewmodel. Things like this:

    case 37:
        @if (Model.GoLeft)
        { 

Here I get a syntax error and the words "expected constant" for Model. Is there some solution to this? Do 开发者_StackOverflow社区I need to upgrade something so it works?

I checked around on stackoverflow. Someone else suggested that I should separate my js but that doesn't help me as for example in this case where I want the keypress to do something if on a certain type of page where the Model allows it. If the js is in another file I can't code this way.

thanks


your approach is just wrong. Dont generate JS code in views by ifs. You defintely should keep your JS separetly (so that browser can efectively cache and reuse it). If you need to change behaviour of client-side code according to model values, do that by generation only some king of "flags" (JS have multiple ways to do that, i am not expert in JS - for example global variable works always, but there are more elegant and recommended ways) and in your client-side method test for their presence and fork your code by that.

0

精彩评论

暂无评论...
验证码 换一张
取 消