<?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>SquaredRoot &#187; how-to</title>
	<atom:link href="http://www.squaredroot.com/tag/how-to/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.squaredroot.com</link>
	<description>.Net Development in DC</description>
	<lastBuildDate>Sun, 16 Aug 2009 01:30:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>MVC: How Do I RedirectToAction While Passing a Parameter?</title>
		<link>http://www.squaredroot.com/2008/04/01/redirecttoaction-with-parameter/</link>
		<comments>http://www.squaredroot.com/2008/04/01/redirecttoaction-with-parameter/#comments</comments>
		<pubDate>Tue, 01 Apr 2008 04:01:13 +0000</pubDate>
		<dc:creator>Troy Goode</dc:creator>
				<category><![CDATA[MVC]]></category>
		<category><![CDATA[how-to]]></category>

		<guid isPermaLink="false">/post/2008/04/01/RedirectToAction-With-Parameter.aspx</guid>
		<description><![CDATA[A common scenario that seems underdocumented to me in the latest MVC release, is how does one use RedirectToAction while passing a value to that action? In the December CTP, you would do this: RedirectToAction( new{ &#160;&#160;&#160;&#160; controller = &#34;Blah&#34;, &#160;&#160;&#160;&#160; action = &#34;Blah&#34;, &#160;&#160;&#160;&#160; foo = &#34;Bar&#34; } ); The new syntax is close, [...]]]></description>
			<content:encoded><![CDATA[<p>A common scenario that seems underdocumented to me in the latest MVC release, is how does one use RedirectToAction while passing a value to that action?</p>
<p>In the December CTP, you would do this:</p>
<blockquote><p>RedirectToAction( new{     <br />&#160;&#160;&#160;&#160; controller = &quot;Blah&quot;,      <br />&#160;&#160;&#160;&#160; action = &quot;Blah&quot;,      <br />&#160;&#160;&#160;&#160; foo = &quot;Bar&quot;      <br />} );</p>
</blockquote>
<p>The new syntax is close, but not easily discoverable. Instead of the above, from now on you should use:</p>
<blockquote><p>RedirectToAction( new RouteValueDictionary(     <br />&#160;&#160;&#160;&#160; new{      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; controller = &quot;Blah&quot;,      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; action = &quot;Blah&quot;,      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; foo = &quot;Bar&quot;      <br />&#160;&#160;&#160;&#160; }      <br />));</p>
</blockquote>
<div style="text-align: center;"><a href="http://www.dotnetkicks.com/kick/?url=http://www.squaredroot.com/2008/04/01/redirecttoaction-with-parameter/" style="border:0; position: relative; top: -2px;"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.squaredroot.com/2008/04/01/redirecttoaction-with-parameter/" style="border:0;" alt="Kick It on DotNetKicks.com" /></a><a href="http://dotnetshoutout.com/Submit?url=http://www.squaredroot.com/2008/04/01/redirecttoaction-with-parameter/" style="border: 0;"><img src="http://dotnetshoutout.com/image.axd?url=http://www.squaredroot.com/2008/04/01/redirecttoaction-with-parameter/" style="border:0px" alt="Shout It on DotNetShoutOuts.com" /></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.squaredroot.com/2008/04/01/redirecttoaction-with-parameter/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>MVC: How Do I Prevent Exposing a Public Method on a Controller?</title>
		<link>http://www.squaredroot.com/2008/03/31/nonaction-on-controller/</link>
		<comments>http://www.squaredroot.com/2008/03/31/nonaction-on-controller/#comments</comments>
		<pubDate>Tue, 01 Apr 2008 00:30:00 +0000</pubDate>
		<dc:creator>Troy Goode</dc:creator>
				<category><![CDATA[MVC]]></category>
		<category><![CDATA[how-to]]></category>

		<guid isPermaLink="false">/post/2008/03/31/NonAction-On-Controller.aspx</guid>
		<description><![CDATA[The purists will say that you shouldn&#39;t have any public methods on a controller that aren&#39;t meant to be exposed via a view or redirect to another action. Assuming that you, like I, have encountered a situation where you need/want to do this, however, here is what you do: [NonAction] public void Foo(){ /* Implementation&#8230; [...]]]></description>
			<content:encoded><![CDATA[<p>
The purists will say that you shouldn&#39;t have any public methods on a controller that aren&#39;t meant to be exposed via a view or redirect to another action. Assuming that you, like I, have encountered a situation where you need/want to do this, however, here is what you do:
</p>
<blockquote>
<p>
	[<strong>NonAction</strong>]       <br />
	<em>public void</em> <strong>Foo</strong>(){ /* <em>Implementation</em>&#8230; */ }
	</p>
</blockquote>
<p>
The <strong>System.Web.Mvc.NonAction</strong> attribute will prevent your public method from being accessed via any routes.</p>
<div style="text-align: center;"><a href="http://www.dotnetkicks.com/kick/?url=http://www.squaredroot.com/2008/03/31/nonaction-on-controller/" style="border:0; position: relative; top: -2px;"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.squaredroot.com/2008/03/31/nonaction-on-controller/" style="border:0;" alt="Kick It on DotNetKicks.com" /></a><a href="http://dotnetshoutout.com/Submit?url=http://www.squaredroot.com/2008/03/31/nonaction-on-controller/" style="border: 0;"><img src="http://dotnetshoutout.com/image.axd?url=http://www.squaredroot.com/2008/03/31/nonaction-on-controller/" style="border:0px" alt="Shout It on DotNetShoutOuts.com" /></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.squaredroot.com/2008/03/31/nonaction-on-controller/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MVC: Strongly Typed ViewData Without A Code-Behind</title>
		<link>http://www.squaredroot.com/2008/01/07/mvc-strongly-type-viewpages-without-a-code-behind/</link>
		<comments>http://www.squaredroot.com/2008/01/07/mvc-strongly-type-viewpages-without-a-code-behind/#comments</comments>
		<pubDate>Tue, 08 Jan 2008 03:00:25 +0000</pubDate>
		<dc:creator>Troy Goode</dc:creator>
				<category><![CDATA[MVC]]></category>
		<category><![CDATA[codebehind]]></category>
		<category><![CDATA[how-to]]></category>

		<guid isPermaLink="false">/post/2008/01/07/MVC-Strongly-Type-ViewPages-Without-A-Code-Behind.aspx</guid>
		<description><![CDATA[If you&#8217;ve tried to use strongly-typed ViewData with a ViewPage or ViewContentPage without using a code-behind file you may have run into the curious scenario of how to specify a generic in the &#60;%@ Page %&#62; element&#8217;s Inherits attribute. The Problem Let&#8217;s say you wanted to specify that the ViewData for this view would be [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve tried to use strongly-typed <strong>ViewData</strong> with a <strong>ViewPage</strong> or <strong>ViewContentPage</strong> without using a code-behind file you may have run into the curious scenario of how to specify a generic in the <strong>&lt;%@ Page %&gt;</strong> element&#8217;s <strong>Inherits</strong> attribute.</p>
<h2>The Problem</h2>
<p>Let&#8217;s say you wanted to specify that the <strong>ViewData</strong> for this view would be an integer. Normally you would specify this in the code-behind like so:</p>
<div class="csharpcode-wrapper">
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:</span> <span class="kwrd">using</span> System;</pre>
<pre class="alteven"><span class="lnum">   2:</span> <span class="kwrd">using</span> System.Web;</pre>
<pre class="alt"><span class="lnum">   3:</span> <span class="kwrd">using</span> System.Web.Mvc;</pre>
<pre class="alteven"><span class="lnum">   4:</span></pre>
<pre class="alt"><span class="lnum">   5:</span> <span class="kwrd">namespace</span> MVC_Example.Views.Home</pre>
<pre class="alteven"><span class="lnum">   6:</span> {</pre>
<pre class="alt"><span class="lnum">   7:</span>     <span class="kwrd">public</span> <span class="kwrd">partial</span> <span class="kwrd">class</span> Index : ViewPage&lt;<span class="kwrd">int</span>&gt;</pre>
<pre class="alteven"><span class="lnum">   8:</span>     {</pre>
<pre class="alt"><span class="lnum">   9:</span>     }</pre>
<pre class="alteven"><span class="lnum">  10:</span> }</pre>
</div>
</div>
<p>Since we don&#8217;t have a code-behind file though, we have to change the .<em>aspx</em> file&#8217;s <strong>&lt;%@ Page %&gt;</strong> element. Trying to set it to the following will not work:</p>
<div class="csharpcode-wrapper">
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:</span> &lt;%@ Page Language=<span class="str">"C#"</span> Inherits=<span class="str">"MVC_Example.Views.Home.Index&lt;int&gt;"</span> %&gt;</pre>
</div>
</div>
<h2>The Basic Answer</h2>
<p>The value of the <strong>Inherits</strong> attribute appears to be delivered straight to the CLR without any language specific parsing, which means we have to specify it the way the CLR wants to interpret it. I browsed around and found (<a href="http://forums.asp.net/t/1193721.aspx">here</a> and <a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=104071&amp;wa=wsignin1.0">here</a>) some information that points out the proper way to do this:</p>
<div class="csharpcode-wrapper">
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:</span> &lt;%@ Page Language=<span class="str">"C#"</span> Inherits=<span class="str">"System.Web.Mvc.ViewPage`1[ [System.Int32,mscorlib] ]"</span> %&gt;</pre>
</div>
</div>
<h2>WTF?</h2>
<p>Eww, huh? The syntax is gnarly, but it gets the job done. Let&#8217;s break the syntax down to make sure you understand what is going on:</p>
<p><strong><em>ParentType`1[ [ChildType,ChildTypeAssembly] ]</em></strong></p>
<ul>
<li><em>&#8220;ParentType&#8221;</em> is fairly obvious. This is the generic object that you want to create a reference to.</li>
<li><em>&#8220;`1[ ["</em> is a bit more obscure. The "`1" specifies the number of generics in this argument, which we'll come back to in a second. The double square brackets are required. A single square bracket will <strong>NOT</strong> work.</li>
<li><em>"ChildType"</em> is the type of object you wish to have the generic consume (what is the proper terminology here? anyone know?).</li>
<li><em>"ChildTypeAssembly"</em> is the name of the assembly (<strong>NOT</strong> the namespace) that contains the <em>ChildType</em>. All of your common value types will be located in "<strong>mscorlib</strong>".</li>
</ul>
<h2>Multiple Generic Types</h2>
<p>Let's say <strong>ViewPage</strong> took two arguments as part of its generic declaration area (ie: <strong>ViewPage&lt;X,Y&gt;</strong>). It doesn't, by the way, but we'll pretend it does for the sake of argument; you may find this information useful someday.</p>
<p>To have two type parameters, you'll have to change the <strong>"`1"</strong> to a <strong>"`2"</strong> and add an extra <strong>"[ChildType, ChildTypeAssembly]&#8220;</strong> reference, separated by a space. More parameters would continue to follow the same logic.</p>
<p>If you wanted to achieve the equivalent of <strong>ViewPage&lt;int,string&gt;</strong> it would look like this:</p>
<div class="csharpcode-wrapper">
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:</span> System.Web.Mvc.ViewPage`2[ [System.Int32,mscorlib], [System.String,mscorlib] ]</pre>
</div>
</div>
<h2>A Generic of Generics</h2>
<p>A more likely scenario is that you may want to have your <strong>ViewData</strong> be a generic <strong>List&lt;T&gt;</strong>. In this case you&#8217;ll just nest the declarations. If you wanted to pass <strong>ViewData</strong> of the type <strong>List&lt;string&gt;</strong> to the <strong>ViewPage</strong> it would look like:</p>
<div class="csharpcode-wrapper">
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:</span> System.Web.Mvc.ViewPage`1[[System.Collections.Generic.List`1[[System.String,mscorlib]], mscorlib]]</pre>
</div>
</div>
<div style="text-align: center;"><a href="http://www.dotnetkicks.com/kick/?url=http://www.squaredroot.com/2008/01/07/mvc-strongly-type-viewpages-without-a-code-behind/" style="border:0; position: relative; top: -2px;"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.squaredroot.com/2008/01/07/mvc-strongly-type-viewpages-without-a-code-behind/" style="border:0;" alt="Kick It on DotNetKicks.com" /></a><a href="http://dotnetshoutout.com/Submit?url=http://www.squaredroot.com/2008/01/07/mvc-strongly-type-viewpages-without-a-code-behind/" style="border: 0;"><img src="http://dotnetshoutout.com/image.axd?url=http://www.squaredroot.com/2008/01/07/mvc-strongly-type-viewpages-without-a-code-behind/" style="border:0px" alt="Shout It on DotNetShoutOuts.com" /></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.squaredroot.com/2008/01/07/mvc-strongly-type-viewpages-without-a-code-behind/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
