ASP.NET MVC Url.Action在jQuery是无法识别 [英] ASP.NET MVC Url.Action in JQuery is not recognized
问题描述
我想用我的js文件Url.Action()方法来定义的网址为我的Ajax调用。到目前为止,我已经失败了。
I am trying to use Url.Action() method in my js file to define urls for my ajax calls. So far I have failed.
$.ajax(
{
type: "POST",
url: '@Url.Action("SomeAction", "SomeController")',
data: {
fileID: rightClickedFileId
},
success: function (data) {
}
});
如果我定义的URL以这种方式,浏览器尝试将数据发布到
If i define the url in this way, browser tries to post data to
http://localhost:5907/FileManager/@Url.Action(%22SomeAction%22,%20%22SomeController%22)
,因此我Ajax调用失败。
and as a result my ajax call fails.
不过,如果我使用/ SomeController / SomeAction
而不是,万物正常工作。
However, If I use '/SomeController/SomeAction'
instead, everythings works fine.
二工程确定,但我想知道与第一个问题呢?难道是由于路由配置?
Second works ok, but I am wondering the problem with first one ? Could it be due to routing configuration ?
感谢。
推荐答案
如果它是一个外部js文件,您的辅助方法不会工作。它应该在剃须刀的观点仅工作。
If it is an external js file,your Helper methods wont work. It should work only in razor views.
你可以做的是使用一个视图 Url.Action
helper方法获取路径,并设置为全局JavaScript变量和使用在外部JS文件。你可以这样做,在Layout.cshtml
What you can do is get the path using Url.Action
helper method in a view and set that to a global javascript variable and use that in your external js file. You may do that in Layout.cshtml
<script type="text/javascript">
var userEditPath="@Url.Action("Edit","User")";
</script>
现在你可以在你的js文件中使用 userEditPath
变量
Now you can use userEditPath
variable in your js file
alert("userEditPath is from Helper method is "+userEditPath)
始终使用的辅助方法。千万不要硬code路径。的
这篇关于ASP.NET MVC Url.Action在jQuery是无法识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!