使用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

查看:136
本文介绍了使用Seprate js文件,并在其中使用的网址助手与ASP.NEt MVC 3的Razor视图引擎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里问类似的问题达林季米特洛夫的答案,我们不能用URL助手像 $阿贾克斯(网址:{url:@ Url.Action(「指数」)', 。在单独的js文件。所以你有什么用URL助手在视图页面,并把它传递给JavaScript的建议,我不想用硬code的url,我需要找到它使用URL帮手。?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆