❔ [STILL UNSOLVED] Forms pointing to the current page retain route parameters - How do I clear them?
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.
Thanks18 Replies
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
bump
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.This is not closed
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.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.What about using
Url.Action
? It allows you to pass only the arguments that you really want
Let me pick an example real quickI really don't want to have to build each link and each form with that
I have done it before for very specific circumstances, but there must surely be a way to clear the route parameters
Put a middleware
That would affect all links
There are several modifiers on the page where it is important to keep those to update things
what about
RouteData.Values.Remove
?Just a few specific ones where I need them cleared
(thank you for responding after so long, by the way)
You can create an [ClearRoute] attribute (custom)
with that piece of code
That seems like it would clear everything for an entire page
same as middleware
and put above the routes you want
My dilemma is I have a partial view which is part of the menu bar for a search box, so it appears on literally every single page, and needs to ensure that all route parameters are cleared when the search form is submitted
Things inside the actual body of the page still need to respect those parameters
Its an annoyingly specific issue unfortunately
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.