Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net MVC: Form with Get Method Losing Parameters on Submit

Say I have a route like this:

"{controller}/{action}/{roomId}/{name}"

And a form action like this (And yes it looks like this in the action in the html ie post server processing):

Room/Index/6/SomeThing?pageNumber=1&amountToShow=5

And the form is simple:

<form action = "Room/Index/6/SomeThing?pageNumber=1&amountToShow=5" method="get">
   <button type="submit">SomeButton</button>
</form>

Now when the button is clicked, the request somehow seems to half lose the pageNumber=1&amountToShow=5 part. In fact, when I looked in the ActionExecutingContext.ActionParameters list, the parameters are there (pageNumber and amountToShow) but there are no values. I even looked in the request, and there are no request parameters despite it knowing the url is "Room/Index/6/SomeThing?pageNumber=1&amountToShow=5".

I thought that maybe this had to do with the button and form, and maybe this wasn't possible but then I adjusted the route to:

"{controller}/{action}/{roomId}/{name}/{pageNumber}/{amountToShow}"

And it works, except the url is super formatted:

 Room/Index/6/SomeThing/1/5

Which is to be expected since it's apparently doing it's job this time. Any ideas?

UPDATE

As suggest below by Adrian Godong, I tried using hidden inputs and it works but this still raises more questions. Why is it that with the more verbose route was able to handle the request parameters without hidden values but the shortened route isn't.

like image 419
Programmin Tool Avatar asked Jan 31 '26 10:01

Programmin Tool


1 Answers

Have you tried moving the query string parameters into hidden input fields?

So, instead of your form block, try this:

<form action = "Room/Index/6/SomeThing" method="get">
    <input type="hidden" name="pageNumber" value="1" />
    <input type="hidden" name="amountToShow" value = "5" />
    <button type="submit">SomeButton</button>
</form>
like image 141
Adrian Godong Avatar answered Feb 03 '26 05:02

Adrian Godong



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!