Andres Elizondo
Andres Elizondo

Reputation: 341

Is it possible to use a dynamic value in asp-route-{value}?

I have Razor Page with the following piece of code to create a link that adds a dynamic value to the route when clicked. This works perfectly.

<a class="page-link" asp-route-systemRightsPage="@(i.Index)">@i.Index</a>

Now, I have different values for the route, in this case I'm using systemRightsPage, but I also have 3 more types that I can navigate to (personsPage, personRightsPage, errorsPage), and so on.

Is it possible to use somehow a dynamic value on the asp-route-{value} part? I tried using this:

<a class="page-link" asp-route-@(RouteValue)="@(i.Index)">@i.Index</a>

This code renders correctly in HTML to:

<a class="page-link" asp-route-personpage="2">2</a>

But the link doesn't work anymore, as nothing happens if I click it.

Has anyone done anything similar?

Upvotes: 2

Views: 1290

Answers (1)

Mike Brind
Mike Brind

Reputation: 30035

You can use the all-route-data attribute to pass arbitrary parameter names and their values:

@{var values = new Dictionary<string,string>{{"personpage", "2"}};}

<a asp-page="/whatever" asp-all-route-data="values">Click</a>

https://www.learnrazorpages.com/razor-pages/tag-helpers/anchor-tag-helper

Upvotes: 2

Related Questions