我正在尝试使用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
发布评论