For many reasons, you'd better place most, if not all of your JS, in separate JS files (so you can use reuse, optimization, browser optimization, content delivery networks, etc.).
To read the result of the server-side firewall code in your JS files, use one of the following methods:
1) Put your razor code in a javascript variable in the view (not verified code)
<script type="text/javascript"> if(!MyGlobalVariables){ MyGlobalVariables = {}; } MyGlobalVariables.IndexUrl = "@Url.Action("Index")"; </script>
2) Use a custom attribute (preferably a data prefix - as suggested in the HTML 5 spec ). See Related discussion here: Can I add a custom attribute to an HTML tag?
<div data-index-url="@Url.Action("Index")"></div>
Then use $ (this) .attr ("data-index-url") in jQuery to access the razor rendering markup.
3) Put C # in hidden input fields on your view and read the hidden input in your JS file.
<input id="indexUrl" type="hidden" value="@Url.Action("Index")" />
To read this in jQuery you have to use $ ("# indexUrl"). val ()
Zaid masud
source share