❔ How to pass anchor tag values to controller to render view based on selected value

HI, how do I pass the values assigned to an anchor tag to the controller so that the controller can render the view or filter out what to show to the user? For example, I have in my views
<a asp-controller="Example" asp-action="Details" value="1">
//
</a>

<a asp-controller="Example" asp-action="Details" value="2">
//
</a>
<a asp-controller="Example" asp-action="Details" value="1">
//
</a>

<a asp-controller="Example" asp-action="Details" value="2">
//
</a>
Controller
[HttpGet]
public IActionResult Summary(int viewsIndicator)
{
return View();
}
[HttpGet]
public IActionResult Summary(int viewsIndicator)
{
return View();
}
I'm not sure how to do something like this in ASP
public IActionResult Summary(int viewsIndicator)
{
if (value === 1) {
// filter out what to show
} else {
// further filtering
}
}
public IActionResult Summary(int viewsIndicator)
{
if (value === 1) {
// filter out what to show
} else {
// further filtering
}
}
6 Replies
undisputed world champions
<a asp-controller="Example" asp-action="Summary" asp-route-viewsIndicator="1"></a> should work, no? https://learn.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/anchor-tag-helper?view=aspnetcore-6.0#asp-route-value
CoreVisional
CoreVisionalOP2y ago
Is asp-route-id and asp-route-{value} the same thing? Seems like I can use either one
undisputed world champions
id is the name of the parameter you want to set you have to replace {value} accordingly 😉
CoreVisional
CoreVisionalOP2y ago
Got it. Btw, since I'm using numbers as indication, is it possible have the url parameter to not show the numbers and instead show something else? Like if the user clicks on All locations, the parameter should be /Location/AllLocations, and not /Location/Summary?viewsIndicator=1
undisputed world champions
use an enum instead? i think that should work out-of-the-box 😉
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server