Razor Syntax and Javascript

前端 未结 1 1815
后悔当初
后悔当初 2020-11-28 05:59

As a test I\'m converting a proof-of-concept app we\'ve written from Web Forms to Razor, simply so we can evaluate it.

I\'ve run into one problem so far that\'s maki

1条回答
  •  清歌不尽
    2020-11-28 06:28

    You need to wrap it in the pseudo element . This will switch the parser back to html mode and it will then parse the javascript as part of the html and not c#. The reason it happens is the @for() is a c# block and anything treated within is also considered c# until it's escaped by an html tag. Since you probably don't want an html tag razor provides the tag to switch modes.

    If you notice the difference in your asp.net webforms you end the <% for line with a %> which takes it out of c# mode. If you download the razor highlighter extension for visual studio 2010 it will help you see when code is treated as code and html is treated as html.

    
    

    Update for latest version

    You can now use the @: syntax for even more readability

    
    

    0 讨论(0)
提交回复
热议问题