I have previously implemented a onclick
event inside a <label>
tag as shown below
<label onclick="window.location='/edit/@(editId)'">edit<lable>
but now, I want to use only tag helpers to generate links using tag helpers like asp-page
, asp-route-editId
etc.
how can I build above onclick url with asp-tag helpers
2
Answers
In this scenario, you have to create the url directly, you can’t use Anchor Tag Helper attributes because they create attributes like
href
for you. But here you have to create the link directly and use it in theonclick
event:if using MVC:
if using Razor Page:
Finally, your code should be as follows(for razor page link)
One important note:
If your input parameter name is
editid
, you can omit the parameter name in route values, but if it has a different name, for example,itemId
, you must explicitly specify the parameter name. As followsIf you want to use anchor tag helper,you need to use
<a></a>
,if you want to remove the underline and change the color,you can try to add the following styles:But in this way,you will have the route like:
So that you need to change the route backend.