Using Razor within JavaScript

后端 未结 12 2305
萌比男神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:

    
    

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

    
    

    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.

提交回复
热议问题