0

I have asp.net mvc project with knockout.js so my index page is getting really huge because of lots of javascript functionality. I'd love to move js code into a separate file but it does not allow me to apply it to the most of the code because if I have something like

$.ajax({
    url: "@Html.Raw(@Url.Action("Load"))",

Then it pops up a error if I move this part of the code into another file. Please advise how can I resolve this issue?

tereško
  • 56,151
  • 24
  • 92
  • 147
Jack Smith
  • 51
  • 1
  • 5

3 Answers3

3

Javascript files are not parsed by ASP.net, so the variables you have of @Html.Raw and @Url.Action("Load") will never be processed.

James Lai
  • 1,997
  • 15
  • 14
0

As @James Lai noted, server side code isn't parsed as such by ASP.Net. See this post for a workaround, or you can pick and choose which scripts can still stay on the page (with server-side code) instead of the "everything" - your choice as to which approach meets your requirements.

Community
  • 1
  • 1
EdSF
  • 10,269
  • 3
  • 40
  • 75
0

Javascript files are not parsed by ASP.NET MVC, thus @Html.Raw(@Url.Action("Load")) will not work in javascript file.

Heres workaround

Instead declare a variable in view.cshtml. In script section as

<script type="text/javascript">
     var actionUrl = '@Url.Action("Load", "Controller")';
</script>

And use actionUrl in javascript file.

Satpal
  • 126,885
  • 12
  • 146
  • 163