81

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

hutchonoid
  • 32,982
  • 15
  • 99
  • 104
mattdwen
  • 5,288
  • 10
  • 47
  • 61

4 Answers4

121

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>

Kiran
  • 56,921
  • 15
  • 176
  • 161
  • 3
    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. – Morten Nørgaard Sep 20 '16 at 06:30
  • 1
    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 methods – Calanus Feb 23 '18 at 14:35
  • @Kiran can I pass object in asp-route dynamic – user123456 Sep 02 '22 at 00:27
  • @user123456 This can be done with a `Dictionary` passed to the `all-route-data` parameter. – CrazyPyro Feb 23 '23 at 17:49
45

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>
Alex White
  • 1,486
  • 16
  • 22
  • This definitely made more sense to me than the others. Can you also have multiple asp-route-{parameter name} in the same ? 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. – Edward Mar 26 '17 at 03:27
  • 1
    Yes, you can. According to http://www.davepaquette.com/archive/2015/06/01/mvc-6-anchor-tag-helper.aspx 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 https://github.com/aspnet/Mvc/issues/4560 – Alex White Mar 29 '17 at 13:52
  • 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. – Edward Mar 29 '17 at 14:57
  • 1
    I've updated my answer to show passing two parameters in the anchor taghelper. Is that better? – Alex White Mar 29 '17 at 23:12
  • 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](http://stackoverflow.com/q/42983224/3527398) about passing lists back. – Edward Mar 29 '17 at 23:37
  • Worked for me. I implemented for pagination – NoloMokgosi Apr 13 '20 at 10:05
  • Worked for me after banging my head trying to figure it out. This is great. – Andrew Reese May 29 '20 at 16:25
5

you can pass custom ID using below code:

<a asp-controller="User" asp-action="Edit" asp-route-id="@item.ID">Edit</a>
Zubayer Bin Ayub
  • 310
  • 3
  • 11
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.

Abdul Hadee
  • 113
  • 6