ASP.Net MVC: Button Post Is Losing QueryString Values And Getting Paramters From A URL

I was going to start this post with a rousing ARE YOU READY FOR SOME PROGRAMMING?, but my lawyer suggested it might cause the NFL to take action against me. He suggested something more simple like PROGRAMMING HAPPENS HERE! since he had high doubts the NBA will sue me. After all they’d have to admit they actually came up with that f-ing slogan in the first place. It’s a win/win situation for me.

So one of the things I’ve run into with MVC is this situation… Say I have a form and a button, and the form has an action that looks like this:

  <form action="/Tools/AddTool/1?redirect=/Tools/ViewTool/1" method="post">
    < button type="submit">GO!</button>

Really simple. The idea is that I want to post to the AddTool action, do some stuff, then have the action redirect back to the ViewTool action. Seems like it should be easy right? Well not so much. Because when you view the ActionParameters (Like in this example) for the “redirect” key, it finds the key but loses the value. This may result in a failure if have an attribute making sure it exists or possiblly you just have it set to a default that’s useless like a fourth foot. Either way, not good.

Now I haven’t figured out the reason for this (Don’t act so surprised, you aren’t a very good actor), but I have figured out a way around it. Basically for the form to keep the values, you need to add hidden values. I know, it’s a pain. Good thing I have a method that makes it easier. Also, this post is a prerequisite for a method I have for creating buttons… but that in the future. ITS A TEASER! THIS POST IS A TEASER! THERE I SAID IT! CAN I GO NOW?

public static String CreateHiddenValuesFromUrl(String baseUrl)
  StringBuilder html = new StringBuilder();

    //Have to find the ? to know where to begin
    Int32 indexOfQuestionMark = baseUrl.IndexOf('?');

    //If the question mark exists and there is something after it, keep going
    if (indexOfQuestionMark > -1 && baseUrl.Length >= indexOfQuestionMark + 1)
      //Get everything AFTER the ?, something I didn't think of first time around
      //Caused many test failures and much shame to my family.
      String request = baseUrl.Substring(indexOfQuestionMark + 1);

      //Cut up the string by the & since every parameter after the first
      //is divided by &.. I know, Duh.
      String[] splitList = request.Split('&');

      //Go through the list of possible request items
      foreach (var requestItem in splitList)
        String[] splitItem = requestItem.Split('=');
        //This is to make sure the parameter actually has a value to send in
        //the hidden values.  I supposed you could create an empty hidden
        //value to preserve the parameter, but remember the parameter
        //isn't being lost from the form action, just the value
        if (splitItem.Length == 2 && !String.IsNullOrEmpty(splitItem[1]))
          //Create a hidden input with the key name and the value
          html.Append("<input type=\"hidden\" name=\"" + splitItem[0] + "\" value=\"" + splitItem[1] + "\" >");

  return html.ToString();

And it’s just that simple. Mind you I could probably make this an extension method for the HtmlHelper, but I don’t really use it for that.

Next up will be more razzle and dazzle using this to make an optional image button. I bet you can’t wait. I know I can.