最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How to pass javaScript variable value in asp-route-id prefix? - Stack Overflow

programmeradmin1浏览0评论

I've an anchor element with the attribute of asp-controller and asp-action. I've a javaScript variable, named g_id that will call the getIdFromUrl function. This function will retrieve the id appended at the end of the url.

/User/Account/5

For example, this function will retrieve the value of 5 from the URL. I want to assign this value to the asp-route-id. However, I am unable to do so.

<a asp-action="ActivityPrice" asp-controller="Activity" asp-route-id = "g_id" class="btn btn-primary" >
Add Activity Price

var g_id = getIdFromURL();

function getIdFromURL() {
    var urlArray = window.location.href.split('/');
    var id = urlArray[urlArray.length - 1];
    return id;
}

I've an anchor element with the attribute of asp-controller and asp-action. I've a javaScript variable, named g_id that will call the getIdFromUrl function. This function will retrieve the id appended at the end of the url.

/User/Account/5

For example, this function will retrieve the value of 5 from the URL. I want to assign this value to the asp-route-id. However, I am unable to do so.

<a asp-action="ActivityPrice" asp-controller="Activity" asp-route-id = "g_id" class="btn btn-primary" >
Add Activity Price

var g_id = getIdFromURL();

function getIdFromURL() {
    var urlArray = window.location.href.split('/');
    var id = urlArray[urlArray.length - 1];
    return id;
}
Share Improve this question asked Jun 15, 2018 at 13:23 IssakiIssaki 1,0642 gold badges21 silver badges37 bronze badges 7
  • Think you are looking for setAttribute: developer.mozilla/en-US/docs/Web/API/Element/setAttribute – Pete Commented Jun 15, 2018 at 13:29
  • Why are you "unable to do so"? What happens when executing the given code? What do you mean by that variable calling a function? – Nico Haase Commented Jun 15, 2018 at 13:31
  • @NicoHaase Hi Nico, basically I am trying to use g_id as the value for asp-route-id – Issaki Commented Jun 15, 2018 at 13:42
  • 1 So, what have you tried to achieve this? There should be tons of tutorials to do this..... – Nico Haase Commented Jun 15, 2018 at 14:13
  • 5 Just a note - Asp.Net will render asp- tag helpers before giving the HTML code to the browser. There shouldn't be any asp- tags in the HTML you view in the browser. asp-action, asp-controller, and asp-route- will all pile into a single href tag for an anchor element. Rather than looking to change the asp-route-id tag, look to change the href tag. – user8761909 Commented Jun 15, 2018 at 16:00
 |  Show 2 more ments

4 Answers 4

Reset to default 3
$('a').attr('action', '/Activity/ActivityPrice/' + g_id);

In your Javascript tag yo can define the below function

function setRouteParameter() {
    var newhref=$("#addActivityPrice").attr('href') + '/' + g_id ;     
    $("#addActivityPrice").attr("href", newhref);        
}

and call it onclick of your anchor element

<a id="addActivityPrice" asp-action="ActivityPrice" asp-controller="Activity" asp-route-id = "g_id" class="btn btn-primary" onclick="setRouteParameter()">
Add Activity Price

asp-route-id is translated to formaction. My solution is

var formActionAttr = $("#myBtn").get(0).getAttribute('formaction');
        formActionAttr = formActionAttr.replace("&", "&idLeft=" + "myValue" + "&idRight=" + "myValue" + "&");
        $("#confirmBtn").get(0).setAttribute('formaction', formActionAttr);

You can use attr():

$('a').attr('asp-route-id', g_id);

发布评论

评论列表(0)

  1. 暂无评论