When specifying asp-controller
and asp-action
on a link, what's the syntax for also passing an id attribute?
E.g. If I wanted to link to the edit URL for a given object, the required URL would be /user/edit/5
for example.
Is there a method to achieve this using TagHelpers, or do we still have to fall back to @Html.ActionLink()
?
I'd like to suggest a combination of the other two answers, but with a bit of extra clarification.
You will use an attribute prefix
asp-route-{name}
where{name}
is the name of the route parameter you want to use. In other words, if the number 5 in your route is passed into the controller as an ID value, you could have:or if the parameter you wanted to pass to the route was item.UserName then
And if you had both parameters then
you can pass custom ID using below code:
You can use the attribute prefix
asp-route-
to prefix your route variable names.Example:
<a asp-action="Edit" asp-route-id="10" asp-route-foo="bar">Edit</a>