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 renderasp-
tag helpers before giving the HTML code to the browser. There shouldn't be anyasp-
tags in the HTML you view in the browser.asp-action
,asp-controller
, andasp-route-
will all pile into a singlehref
tag for an anchor element. Rather than looking to change theasp-route-id
tag, look to change thehref
tag. – user8761909 Commented Jun 15, 2018 at 16:00
4 Answers
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);