Search code examples
c#asp.net-coreasp.net-core-3.0tag-helpers

Use selected option values for asp-route in .NET Core


I want to have two select forms that pass their selected values to the URL via a GET request.

Can I do this with only Tag Helpers? Or do I need to use JavaScript?

Here is the form code from the view

<form asp-action="Index" asp-route-startyear="@selectedValue1" asp-route-endyear="@selectedValue2" method="get">

And here is the controller that it is calling

public IActionResult Index(int startYear = 1978, int endYear = 0)

And finally this is the route from the startup.cs file

        routes.MapControllerRoute(
            name: "StandingsByYear",
            pattern: "{startyear:int}/{endyear:int}",
            defaults: new { controller = "Home", action = "Index" });

I can make add an onclick() to the submit button that changes the window.location.href using JavaScript but was wondering if there was in Tag Helper magic that could pull off the same thing.

Thanks!


Solution

  • Tag Helpers enable server-side code to participate in creating and rendering HTML elements . So that if you want to bind client side value(from select) , tag helper won't work and you need to dynamically change the url in Javascript .