使用Seprate js文件,并在其中使用的网址助手与ASP.NEt MVC 3的Razor视图引擎 [英] Use Seprate js File And use Url Helpers in it with ASP.NEt MVC 3 and Razor View Engine
本文介绍了使用Seprate js文件,并在其中使用的网址助手与ASP.NEt MVC 3的Razor视图引擎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I ask a similar question here and Darin Dimitrov
answer that we can't use Url helper like $.ajax({ url: '@Url.Action("Index")', . . .
in separate js file so what is your suggestion to use Url helper in view page and pass it to javascript, I don't want to use hard code url, I need to find it with Url helper.?
推荐答案
使用一个隐藏字段来存储您的网址,然后使用JavaScript来读取隐藏字段,然后使用你的code。这样,你可以保持JS文件单独的视图。事情是这样的:
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 , . . .
这篇关于使用Seprate js文件,并在其中使用的网址助手与ASP.NEt MVC 3的Razor视图引擎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文