TagHelper for passing route values as part of a link
Asked Answered
B

4

85

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()?

Bioecology answered 4/5, 2015 at 0:14 Comment(0)
W
124

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>

Wreckage answered 4/5, 2015 at 22:54 Comment(4)
I couldn't get this to work, because my "asp-route-foobar" didn't match my route: "routes.MapRoute("yearNoCombo", template: "{controller=Home}/{action=Index}/{foobar?}/{whatever1}/{whatever2}"); - the route-names must match. HTH others.Regular
In addition to the above comment you do need to ensure that your routes are setup correctly to avoid the route bit appearing as a parameter in the HTML Either using MapRoute or route annotations on your methodsAccretion
@Wreckage can I pass object in asp-route dynamicRiddell
@Riddell This can be done with a Dictionary<string, string> passed to the all-route-data parameter.Nonexistence
F
47

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:

<a asp-controller="User" asp-action="Edit" asp-route-id="@item.ID">Edit</a>

or if the parameter you wanted to pass to the route was item.UserName then

<a asp-controller="User" asp-action="Edit" asp-route-username="@item.UserName">Edit</a>

And if you had both parameters then

<a asp-controller="User" asp-action="Edit" asp-route-id="@item.Id" asp-route-username="@item.UserName">Edit</a>
Fully answered 5/3, 2017 at 0:40 Comment(7)
This definitely made more sense to me than the others. Can you also have multiple asp-route-{parameter name} in the same <a></a> ? This helped my issue with it Not binding properly trying to send a foreign key CompanyId back as it was using Id and placing it in the Order primary key Id instead.Debbee
Yes, you can. According to davepaquette.com/archive/2015/06/01/… You can specify values for as many parameters as you need using attributes with the asp-route- prefix. I don't think you can have two parameters with the same name - there is a feature request/issue about that here github.com/aspnet/Mvc/issues/4560Fully
So if I read that right, currently there is a way to pass lists only in the action helper method and not in the anchor tag helpers.Debbee
I've updated my answer to show passing two parameters in the anchor taghelper. Is that better?Fully
Didn't really need to. I of course didn't know of the terminology used in your links when I was seeking answers to my question about passing lists back.Debbee
Worked for me. I implemented for paginationCandida
Worked for me after banging my head trying to figure it out. This is great.Schacker
B
5

you can pass custom ID using below code:

<a asp-controller="User" asp-action="Edit" asp-route-id="@item.ID">Edit</a>
Boresome answered 23/11, 2016 at 12:40 Comment(0)
P
1

I would suggest one more way for Razor view to use model value using @Html.actionlink in jsp

var URLvalue='@Html.ActionLink("UserString", "action", new { routeValueName1 = "__value1__", routeValueName2="__value2__" }, htmlAttributes: new { @class = "btn btn-default", @role = "button" })'
     .replace('__value1__', Model.somevalue).replace('__value2__',  Model.somevalue);

you can use URLvalue where ever you want to in jsp or jquery.

Placebo answered 24/8, 2020 at 7:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.