Use Separate js File And use Url Helpers in it with ASP.NEt MVC 3 and Razor View Engine

前端 未结 6 969
太阳男子
太阳男子 2020-12-15 06:22

I ask a similar question here and Darin Dimitrov answer that we can\'t use Url helper like $.ajax({ url: \'@Url.Action(\"Index\")\', . . . in sepa

6条回答
  •  一向
    一向 (楼主)
    2020-12-15 06:56

    Use a hidden field to store your url, then use javascript to read the hidden field, then use that in your code. That way you can keep the JS file separate to the view. Something like this:

    //In Your View
        @Html.Hidden("MyURL", Url.Action("Index"))
    
    //In Your JS
        var myUrl = $("#MyURL").val();
    
        $.ajax({ url: myUrl , . . .
    

提交回复
热议问题