在JavaScript中使用Url.Action

编程入门 行业动态 更新时间:2024-10-24 12:31:31
本文介绍了在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

本文发布于:2023-11-03 20:35:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1556024.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:JavaScript   Url   Action

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!