CinnamonBun
CinnamonBun

Reputation: 1150

Using razor within script tags

I seem to be having an issue with using razor syntax within a javascript block. The following compiles, however, I'm getting a syntax error warning.

An example of when this happens:

@section scripts{
    <script type="text/javascript">
        var rating = @Model.Rating;
    </script>
} 

Upvotes: 1

Views: 550

Answers (1)

SLaks
SLaks

Reputation: 888203

That's a false positive from the editor, which doesn't recognize Razor in JS.
Ignore it.

However, if you use this approach for textual data, make sure to Javascript-escape it, or ou will get runtime JS errors & XSS vulnerabilities.

Upvotes: 3

Related Questions