Alex Rivera | Logout

Better solution to using Url.Action in javascript code

Asked 2011-01-24T11:42:43.843
22

In my current project using asp.net MVC 3 (using razor), when I'm making Ajax calls, I have to keep the JS on the view page because I want to use Url.Action to generate the URL. This means I can't split the js code out into .JS files, Is there a better solution than what I'm currently doing.

Edit
Report

1 Answer

1

your javascript code should be reside in separate javascript file. you can use global variables to link your view and javascript. E.g. view page looks like below.

view html ...
<script type="text/javascript">
var pageUrl = @GetURL()
</script>

<script type="text/javascript" src="/js/script.js"/>

script.js file use the pageUrl variable and other global variables.

answered 2011-01-24T11:52:34.590

Your Answer