简体   繁体   English

在Razor中使用jquery变量

[英]Using jquery variable in Razor

I have a table in which there are input buttons with attribute data. 我有一个表,其中有带有属性数据的输入按钮。 I want to read the value of this attribute and pass it on to razor's routeurl. 我想读取此属性的值并将其传递给razor的routeurl。

My jquery function: 我的jQuery函数:

$('#table').on("click", "input.myClass", function () {
    var abc = $(this).attr('data');
    var myroute = @Url.RouteUrl("Routename", abc)-- > It says abc not defined
        //use myroute  
});

what i have experience you can't use js variable in razor but vica vera is possible. 我有什么经验,你不能在剃须刀中使用js变量,但vica vera是可能的。

so You have to achieve it by doing something like this: 因此,您必须通过执行以下操作来实现它:

var myroute = '@Url.RouteUrl("Routename")';

myroute = myroute +"/"+ abc;

Suppose route rendered by RouteUrlHelper is: 假设由RouteUrlHelper呈现的路由为:

var myroute =   '/Home/About';
var abc = 1;

concatenate value with it: 将值与其连接:

 myroute = myroute +"/"+ abc;// --->  /Home/About/1

Another method I use is this one: 我使用的另一种方法是:

var myroute = "@Url.RouteUrl("Routename","-abc")";
myroute = myroute.replace("-abc", abc);
  1. You create your url o route with a dummy text parameter with a "-" (in your case "-abc") 您使用带有“-”(在您的情况下为“ -abc”)的虚拟文本参数创建url路由。
  2. Then you replace where is "-abc" with your actual abc javascript variable 然后,用实际的abc javascript变量替换“ -abc”在哪里
  3. You can use it with the value now available. 您可以将其与现在可用的值一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM