Using Razor within JavaScript

后端 未结 12 2303
萌比男神i
萌比男神i 2020-11-22 04:12

Is it possible or is there a workaround to use Razor syntax within JavaScript that is in a view (cshtml)?

I am trying to add markers to a Google map...

相关标签:
12条回答
  • 2020-11-22 05:00

    A simple and a good straight-forward example:

    <script>
        // This gets the username from the Razor engine and puts it
        // in JavaScript to create a variable I can access from the
        // client side.
        //
        // It's an odd workaraound, but it works.
        @{
            var outScript = "var razorUserName = " + "\"" + @User.Identity.Name + "\"";
        }
        @MvcHtmlString.Create(outScript);
    </script>
    

    This creates a script in your page at the location you place the code above which looks like the following:

    <script>
        // This gets the username from the Razor engine and puts it
        // in JavaScript to create a variable I can access from
        // client side.
        //
        // It's an odd workaraound, but it works.
    
        var razorUserName = "daylight";
    </script>
    

    Now you have a global JavaScript variable named razorUserName which you can access and use on the client. The Razor engine has obviously extracted the value from @User.Identity.Name (server-side variable) and put it in the code it writes to your script tag.

    0 讨论(0)
  • That will work fine, as long as it's in a CSHTML page and not an external JavaScript file.

    The Razor template engine doesn't care what it's outputting and does not differentiate between <script> or other tags.

    However, you need to encode your strings to prevent XSS attacks.

    0 讨论(0)
  • 2020-11-22 05:01

    One thing to add - I found that Razor syntax hilighter (and probably the compiler) interpret the position of the opening bracket differently:

    <script type="text/javascript">
        var somevar = new Array();
    
        @foreach (var item in items)
        {  // <----  placed on a separate line, NOT WORKING, HILIGHTS SYNTAX ERRORS
            <text>
            </text>
        }
    
        @foreach (var item in items) {  // <----  placed on the same line, WORKING !!!
            <text>
            </text>
        }
    </script>
    
    0 讨论(0)
  • 2020-11-22 05:02

    There is also one more option than @: and <text></text>.

    Using <script> block itself.

    When you need to do large chunks of JavaScript depending on Razor code, you can do it like this:

    @if(Utils.FeatureEnabled("Feature")) {
        <script>
            // If this feature is enabled
        </script>
    }
    
    <script>
        // Other JavaScript code
    </script>
    

    Pros of this manner is that it doesn't mix JavaScript and Razor too much, because mixing them a lot will cause readability issues eventually. Also large text blocks are not very readable either.

    0 讨论(0)
  • 2020-11-22 05:06

    I prefer "<!--" "-->" like a "text>"

    <script type="text/javascript">
    //some javascript here     
    
    @foreach (var item in itens)
    {                 
    <!--  
       var title = @(item.name)
        ...
    -->
    
    </script>
    
    0 讨论(0)
  • 2020-11-22 05:08

    I finally found the solution (*.vbhtml):

    function razorsyntax() {
        /* Double */
        @(MvcHtmlString.Create("var szam =" & mydoublevariable & ";"))
        alert(szam);
    
        /* String */
        var str = '@stringvariable';
        alert(str);
    }
    
    0 讨论(0)
提交回复
热议问题