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;
}
7
  • Think you are looking for setAttribute: developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute 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? 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 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..... 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 compile 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. Commented Jun 15, 2018 at 16:00

4 Answers 4

3
$('a').attr('action', '/Activity/ActivityPrice/' + g_id);
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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);

Comments

-1

You can use attr():

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

1 Comment

This will add an attribute named 'asp-route-id' but will not change the link href. This is explained in the question comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.