﻿<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Programming By A Tool &#187; QueryString</title>
	<atom:link href="http://byatool.com/tag/querystring/feed/" rel="self" type="application/rss+xml" />
	<link>http://byatool.com</link>
	<description>This is my recorded stumbling through ASP.Net, Framework 3.5, C# 3.0, Ajax, Javascript, and Love.  Stay, learn, destroy.  It's your life.</description>
	<lastBuildDate>Thu, 03 May 2012 17:28:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>ASP.Net MVC: Button Post Is Losing QueryString Values And Getting Paramters From A URL</title>
		<link>http://byatool.com/utilities/asp-net-mvc-button-post-is-losing-querystring-values-and-getting-paramters-from-a-url/</link>
		<comments>http://byatool.com/utilities/asp-net-mvc-button-post-is-losing-querystring-values-and-getting-paramters-from-a-url/#comments</comments>
		<pubDate>Thu, 13 Aug 2009 14:43:18 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Utilities]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[QueryString]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=838</guid>
		<description><![CDATA[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&#8217;d have to admit [...]]]></description>
			<content:encoded><![CDATA[<p>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&#8217;d have to admit they actually came up with that f-ing slogan in the first place. It&#8217;s a win/win situation for me.</p>
<p>So one of the things I&#8217;ve run into with MVC is this situation&#8230; Say I have a form and a button, and the form has an action that looks like this:</p>
<pre>  &lt;<span style="color: #800000;">form</span> <span style="color: #ff0000;">action</span><span style="color: #0000ff;">="/Tools/AddTool/1?redirect=/Tools/ViewTool/1"</span> <span style="color: #ff0000;">method</span><span style="color: #0000ff;">="post"</span>&gt;
    &lt; <span style="color: #ff0000;">button</span> <span style="color: #ff0000;">type</span><span style="color: #0000ff;">="submit"</span>&gt;GO!&lt;/<span style="color: #800000;">button</span>&gt;
  &lt;/<span style="color: #800000;">form</span>&gt;</pre>
<p>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 (<a href="http://byatool.com/index.php/c/asp-net-mvc-attributes-and-semi-dynamic-check-for-request-parameters/">Like in this example</a>) for the &#8220;redirect&#8221; 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&#8217;s useless like a fourth foot. Either way, not good.</p>
<p>Now I haven&#8217;t figured out the reason for this (Don&#8217;t act so surprised, you aren&#8217;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&#8217;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&#8230; but that in the future.  ITS A TEASER! THIS POST IS A TEASER!  THERE I SAID IT!  CAN I GO NOW?</p>
<pre><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #008080;">String</span> CreateHiddenValuesFromUrl(<span style="color: #008080;">String</span> baseUrl)
{
  <span style="color: #008080;">StringBuilder</span> html = <span style="color: #0000ff;">new</span> <span style="color: #008080;">StringBuilder</span>();

  <span style="color: #0000ff;">if</span>(!<span style="color: #008080;">String</span>.IsNullOrEmpty(baseUrl))
  {
    <span style="color: #008000;">//Have to find the ? to know where to begin</span>
    <span style="color: #008080;">Int32</span> indexOfQuestionMark = baseUrl.IndexOf(<span style="color: #800000;">'?'</span>);

    <span style="color: #008000;">//If the question mark exists and there is something after it, keep going
</span>    <span style="color: #0000ff;">if</span> (indexOfQuestionMark &gt; -1 &amp;&amp; baseUrl.Length &gt;= indexOfQuestionMark + 1)
    {
      <span style="color: #008000;">//Get everything AFTER the ?, something I didn't think of first time around
</span>      <span style="color: #008000;">//Caused many test failures and much shame to my family.
</span>      <span style="color: #008080;">String</span> request = baseUrl.Substring(indexOfQuestionMark + 1);

      <span style="color: #008000;">//Cut up the string by the &amp; since every parameter after the first
</span>      <span style="color: #008000;">//is divided by &amp;.. I know, Duh.
</span>      <span style="color: #008080;">String</span>[] splitList = request.Split(<span style="color: #800000;">'&amp;'</span>);

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

  <span style="color: #0000ff;">return</span> html.ToString();
}</pre>
<p>And it&#8217;s just that simple. Mind you I could probably make this an extension method for the HtmlHelper, but I don&#8217;t really use it for that.</p>
<p>Next up will be more razzle and dazzle using this to make an optional image button. I bet you can&#8217;t wait. I know I can.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/utilities/asp-net-mvc-button-post-is-losing-querystring-values-and-getting-paramters-from-a-url/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

