在JavaScript中使用Url.Action [英] Using Url.Action in javascript

查看:212
本文介绍了在JavaScript中使用Url.Action的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Url.Action方法正确生成ajax调用所需的Url,但是在尝试构建RouteValues时遇到了问题,这是代码的问题所在:

I am trying to use the Url.Action method to correctly generate the required Url for an ajax call but I'm running into problems when trying to build the RouteValues, here's the problem line of code:

var url = @Url.Action("ViewFile", "Default", new { itemId = $(this).data("itemid") });

如您所见,我正在尝试分配JQuery $(this).data("itemid")的结果 到RouteValues中的itemId.

As you can see, I'm trying to assign the result of the JQuery $(this).data("itemid") to itemId in the RouteValues.

有没有一种使用剃刀语法的方法可以编译该代码?

Is there a way using razor syntax which will allow this code to compile?

推荐答案

您正在使客户端与服务器端混淆.尝试这样的事情:

You are confusing client side with server side. Try something like this:

var url = '@Url.Action("ViewFile", "Default")?itemId=' + $(this).data("itemid");

当使用razor视图引擎编写@时,您正在编写一条指令,该指令将在此命令的结尾在服务器端进行处理.在您的情况下,您想在来自javascript的url中添加一个参数,因此,只需在客户端将值与@Url助手生成的url相连即可.

when you write a @ with razor view engine, you are writing a instruction that will be process on the server side at the end of this command. In your case you want to add a parameter in the url that comes from the javascript, so, just concat on the client side the value with the url generated by the @Url helper.

PS:我假设您正在使用默认路由表.

这篇关于在JavaScript中使用Url.Action的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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