<?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>Ten Tonne Baby &#187; forms asp.net mvc</title>
	<atom:link href="http://www.tentonnebaby.com/tag/forms-aspnet-mvc/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tentonnebaby.com</link>
	<description>Discussion on Web Technologies, Design and London</description>
	<lastBuildDate>Thu, 19 Jan 2012 09:33:30 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Determining which button submitted the form &#8211; ASP.NET MVC</title>
		<link>http://www.tentonnebaby.com/2008/11/03/determining-which-button-submitted-the-form-aspnet-mvc/</link>
		<comments>http://www.tentonnebaby.com/2008/11/03/determining-which-button-submitted-the-form-aspnet-mvc/#comments</comments>
		<pubDate>Mon, 03 Nov 2008 10:39:02 +0000</pubDate>
		<dc:creator>Oli</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[forms asp.net mvc]]></category>

		<guid isPermaLink="false">http://www.embeddedstream.com/2008/11/03/determining-which-button-submitted-the-form-aspnet-mvc/</guid>
		<description><![CDATA[There are some scenarios where there are multiple buttons on a form, and you need to carry out different logic / validation according to which button was clicked. For example, look at the form sample below. 
 
Clicking the upload button should just deal with the file upload logic, whereas clicking &#8216;Next&#8217; should perform the [...]]]></description>
			<content:encoded><![CDATA[<p>There are some scenarios where there are multiple buttons on a form, and you need to carry out different logic / validation according to which button was clicked. For example, look at the form sample below. </p>
<p><a href="http://www.embeddedstream.com/wp-content/uploads/2008/11/formexample-03.jpg"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="252" alt="formExample_03" src="http://www.embeddedstream.com/wp-content/uploads/2008/11/formexample-03-thumb.jpg" width="334" border="0"></a> </p>
<p>Clicking the upload button should just deal with the file upload logic, whereas clicking &#8216;Next&#8217; should perform the main validation for the form etc. Working with webforms there are a couple of ways to implement this &#8211; Validation groups on validator controls, and specific click handlers for the buttons on a code-behind page.</p>
<p>Working with the MVC framework, there are two main options &#8211; either submitting the form to two different end-points, or having a single endpoint to handle the post with conditional logic.</p>
<p>Providing two separate action methods is nice and clean, but means the browser will end up pointing to a new address after completing the post. Not ideal.</p>
<p>You can implement a single action method running from the same address as the main GET request handler by using the AccectVerbs attribute on the action method. E.g.</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4">
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   1:</span> [ActionName(<span style="color: #006080">"Index"</span>), AcceptVerbs(HttpVerbs.Get)]</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> ActionResult FormGet()</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   3:</span> {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   4:</span>     <span style="color: #008000">// blah</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   5:</span> }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   6:</span>&nbsp; </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   7:</span> [ActionName(<span style="color: #006080">"Index"</span>), AcceptVerbs(HttpVerbs.Post)]</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   8:</span> <span style="color: #0000ff">public</span> ActionResult FormSubmit(<span style="color: #0000ff">byte</span>[] photo, <span style="color: #0000ff">string</span> title, <span style="color: #0000ff">string</span> description)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   9:</span> {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  10:</span>    <span style="color: #008000">// blah</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  11:</span> }</pre>
</div>
</div>
<p>If you need to know which button was clicked, and perform appropriate logic, start by defining a named input control that will submit the form. This could be &lt;input type=&#8221;submit&#8221;&gt;, &lt;input type=&#8221;image&#8221;&gt;, or &lt;button&gt;. When one of these controls is used to submit a form, the form data will include the name of the control as both the key and value.</p>
<p>On the signature for your action method, provide a string parameter matching the name of the input control. If the button was clicked, the parameter value will be the name of the form control, otherwise the value will be null.</p>
<p>This will allow you to provide a single endpoint that implements different logic according to which button was clicked.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tentonnebaby.com/2008/11/03/determining-which-button-submitted-the-form-aspnet-mvc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

