使用单独的 js 文件并在其中使用 Url Helpers 和 ASP.NEt MVC 3 和 Razor View Engine [英] Use Separate js File And use Url Helpers in it with ASP.NEt MVC 3 and Razor View Engine

查看:16
本文介绍了使用单独的 js 文件并在其中使用 Url Helpers 和 ASP.NEt MVC 3 和 Razor View Engine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里问了一个类似的问题Darin Dimitrov 回答说我们不能单独使用像 $.ajax({ url: '@Url.Action("Index")', . . . 这样的 Url helperjs 文件,那么您建议在查看页面中使用 Url 助手并将其传递给 javascript,我不想使用硬编码 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.?

推荐答案

使用隐藏字段来存储您的 url,然后使用 javascript 读取隐藏字段,然后在您的代码中使用它.这样您就可以将 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 , . . .

这篇关于使用单独的 js 文件并在其中使用 Url Helpers 和 ASP.NEt MVC 3 和 Razor View Engine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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