Forms pointing to the current page retain route parameters - How do I clear them?

ahorner

Member
Joined
May 4, 2023
Messages
6
Location
United Kingdom
Programming Experience
5-10
I have a controller action which loads a view. The contents of the view are paginated by a route parameter and some pagination controls. These work fine.

There is a search box which allows you to filter or refilter results. This search box is a form which does a GET to the exact same action. Essentially, the page initial load, pagination, and search box all GET the exact same action.

If I move to any page, let's say 5 for this example, then everything loads fine, but the form URL generated by asp-controller asp-action on the form points to the current action with the page route parameter set (the route parameter is part of the URL, like /Stuff or /Stuff/0 for first page and /Stuff/5 for 5th page)

This means clicking Search to get new results automatically lands on Page 5. How can I correctly clear the previous route parameters? I would very much prefer not having to do asp-route-page="0". There could be many other parameters for filtering introduced later which I do not want to have to explicitly clear.

I would prefer to not have to use Url.Action to generate the target links instead of the tag helper, and I also don't want to use RouteData.Values.Remove as this would clear parameters within the current scope (breaking other forms which still need these parameters)

Thanks
 
Show us how you are currently using the tag helper to generate your search results.
 
Show us how you are currently using the tag helper to generate your search results.

Just a simple standard one:
HTML:
<form id="nav-search-form" style="display: none;" method="GET" asp-controller="Home" asp-action="Search"></form>

<!-- Elswhere in the page: -->
<input type="text" form="nav-search-form" />
<button type="submit" form="nav-search-form">Search</button>
 
That's your submit button. I am asking about the search results.
 
That's your submit button. I am asking about the search results.

I'm not using a tag helper to generate search results, its just a foreach loop iterating over items provided as part of the view model generating static HTML. Its large and complex because theres several sub parts for generating different types of elements.

The issue I have is not at all related to the generation of content, it is related to the removal of route parameters when submitting a form or clicking a link when using the form or anchor tag helpers for route generation (action on forms, href on anchors)
 
Oh. I missed that. My first reading of your post was that each of your search results links was the one that the page numbers as part of the URL.

I didn't dig too deeply, but my quick scan of this tutorial seems to suggest that the page number is not part of the URL, but rather part of the model that gets passed around. It also seems to lay down the strategy for how to have search criteria.

 
Oh. I missed that. My first reading of your post was that each of your search results links was the one that the page numbers as part of the URL.

I didn't dig too deeply, but my quick scan of this tutorial seems to suggest that the page number is not part of the URL, but rather part of the model that gets passed around. It also seems to lay down the strategy for how to have search criteria.


In this tutorial, they're also using a route parameter as defined by asp-route-pageNumber.

The page number index is only one single example I provided. There's a lot of filtering options which can be applied to the current displayed search which are route parameters handled as query parameters. If you change a filter option, the page refreshes with the filter option set as a query parameter which is used to generate the new set of results.

These filters are also affected by my issue.

If, for example, I am on page 5 with filter A, B and C all set to specific values, and then I submit the HTML form (as shown in my previous post) then the page number and all three filters are retained. I want to clear them as part of submitting a brand new search.

If this is still unclear, I am more than happy to put together a small demo application tomorrow which demonstrates the issue fully.

Thanks!
 
Minimal repo is always very good to have because it lets people jump in and try stuff.
 
Back
Top Bottom