简体   繁体   中英

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.

My jquery function:

$('#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.

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:

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")
  2. Then you replace where is "-abc" with your actual abc javascript variable
  3. You can use it with the value now available.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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