ASP.Net MVC Issue: Form With Get Method Not Passing Request Values

DISCLAIMER: This has a solution but there has to be a better way.

So I came across a really odd situation with the MVC framework today, and I’m on the edge of some serious Nerd Rage. Here’s the issue:

I have a route that looks like this:

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

And a form that looks like this:

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

Can’t get much simpler… or is it more simple? Whatever. Point is, when the the button is clicked, it should go to that page like the form action says to. And it does, sort of. For some reason it ends up stripping the values from the pageNumber and amoutToShow request variables. How do I know this? Well break points at certain times show me that the current ActionExecutingContext ActionParameters (the thing that stores all the request parameters) has the two I need but neither has a value. And since they are not included in that route description, it must see them in the url.

You might think it has to do with forms and submitting, and it might except here’s the kicker… with a route like this:

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

All of a sudden it can see the values. Mind you the url ends up like:

  Room/Index/1/Something/1/5

Which you would expect from the route definition. To add to the fun, if I add hidden values:

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

It works with the original route. Doesn’t really make sense to me, but my understanding of the system isn’t exactly expert level. However, I could buy this if the hidden field method worked and the extended route definition didn’t.

For now I can use the hidden input method since there’s no issue with showing the parameters. Just not what I’d call “Best solution” types stuff.

Sigh. MVC : One step forward, two steps back.

3 thoughts on “ASP.Net MVC Issue: Form With Get Method Not Passing Request Values”

  1. I think it gets munged cause when you submit a form via get it appends ‘?formvalue=value&otherfield=annothervalue’ to the request string. This may cause your ‘?stuff=value’ to get ignored by IIS/ASP. Just a wild guess.

  2. I had the same problem time ago and i solve it when use the Post method although i set route parameters. I don’t know if it’s the best approach, but it works for me.
    Example:
    @using (Html.BeginForm(“ShowUserArticles”, “Articles”, new { articleType = Model.ArticleType, userID = Model.UserID, category = Model.SelectedCategory }, FormMethod.Post))
    {…}

Comments are closed.