<?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>David Laing's blog &#187; ASP.NET MVC</title>
	<atom:link href="http://davidlaing.com/category/platforms/aspnet-mvc/feed/" rel="self" type="application/rss+xml" />
	<link>http://davidlaing.com</link>
	<description>Craftmanship over crap</description>
	<lastBuildDate>Mon, 19 Jul 2010 00:39:10 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9-rare</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>ASP.NET MVC Beta &#8211; Setting properties on ViewControls</title>
		<link>http://davidlaing.com/2008/11/27/aspnet-mvc-beta-setting-properties-on-viewcontrols/</link>
		<comments>http://davidlaing.com/2008/11/27/aspnet-mvc-beta-setting-properties-on-viewcontrols/#comments</comments>
		<pubDate>Thu, 27 Nov 2008 08:49:52 +0000</pubDate>
		<dc:creator>mrdavidlaing</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[ViewData]]></category>
		<category><![CDATA[ViewUserControl]]></category>

		<guid isPermaLink="false">http://davidlaing.com/?p=102</guid>
		<description><![CDATA[In ASP.NET MVC Beta, it isn&#8217;t possible to set properties on partials when calling them with Html.RenderPartial.
Rusty Zarse blogged about a useful ViewData helper class, which allows you to set properties by passing values to the partial through the ViewData.
I&#8217;ve extended this slightly to enable the following syntax:

[-]?View Code CSHARP1
2
3
4
5
6
7
8
&#60;% Html.RenderPartial&#40;&#34;YUIDataTable&#34;,
      [...]]]></description>
			<content:encoded><![CDATA[<p>In ASP.NET MVC Beta, it isn&#8217;t possible to set properties on partials when calling them with Html.RenderPartial.</p>
<p><a href="http://www.vitaminzproductions.com/technology-blog/index.php/2008/11/12/setting-properties-using-aspnet-mvc/" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.vitaminzproductions.com/technology-blog/index.php/2008/11/12/setting-properties-using-aspnet-mvc/');">Rusty Zarse</a> blogged about a useful ViewData helper class, which allows you to set properties by passing values to the partial through the ViewData.</p>
<p>I&#8217;ve extended this slightly to enable the following syntax:</p>

<div id="wp_codebox_msgheader"><span class="right"><a href="javascript:;" onclick="toggle_collapse('p1024');">[<span id="p1024_symbol">-</span>]</a><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p102code4'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div id="wp_codebox"><table width="100%" ><tr id="p1024"><td width="1%" class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p102code4"><pre class="csharp"><span style="color: #008000;">&lt;%</span> Html.<span style="color: #0000FF;">RenderPartial</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;YUIDataTable&quot;</span>,
               ViewDataDictionaryBuilder.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span>
                      <span style="color: #008000;">new</span>
                      <span style="color: #000000;">&#123;</span>
                        DataTableId <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;QuoteDataTable&quot;</span>,
                        ConfigNamespace <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;QuoteDataTableConfig&quot;</span>,
                        HideFilter <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span>
                       <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;%&gt;</span></pre></td></tr></table></div>

<p>Which sets properties on a ViewUserControl like this:</p>

<div id="wp_codebox_msgheader"><span class="right"><a href="javascript:;" onclick="toggle_collapse('p1025');">[<span id="p1025_symbol">-</span>]</a><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p102code5'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div id="wp_codebox"><table width="100%" ><tr id="p1025"><td width="1%" class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p102code5"><pre class="csharp">     <span style="color: #0600FF;">public</span> partial <span style="color: #FF0000;">class</span> YUIDataTable <span style="color: #008000;">:</span> ViewUserControl
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> ConfigNamespace <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> DataTableId <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> HideFilter <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">void</span> Page_Load<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            ViewDataDictionaryBuilder.<span style="color: #0000FF;">SetPropertiesToViewDataValues</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Here is the full helper code.</p>

<div id="wp_codebox_msgheader"><span class="right"><a href="javascript:;" onclick="toggle_collapse('p1026');">[<span id="p1026_symbol">-</span>]</a><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p102code6'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div id="wp_codebox"><table width="100%" ><tr id="p1026"><td width="1%" class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
</pre></td><td class="code" id="p102code6"><pre class="csharp"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> MvcHelpers
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">///</span>
    <span style="color: #008080; font-style: italic;">/// With thanks to http://www.vitaminzproductions.com/technology-blog/index.php/2008/11/12/setting-properties-using-aspnet-mvc/</span>
    <span style="color: #008080; font-style: italic;">///</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> ViewDataDictionaryBuilder
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">Mvc</span>.<span style="color: #0000FF;">ViewDataDictionary</span> Create<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> data, ModelType model<span style="color: #000000;">&#41;</span> where ModelType <span style="color: #008000;">:</span> <span style="color: #FF0000;">class</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">System</span>.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">Mvc</span>.<span style="color: #0000FF;">ViewDataDictionary</span><span style="color: #000000;">&#41;</span>CreateInternal<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">Mvc</span>.<span style="color: #0000FF;">ViewDataDictionary</span><span style="color: #000000;">&#40;</span>model<span style="color: #000000;">&#41;</span>, data<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">Mvc</span>.<span style="color: #0000FF;">ViewDataDictionary</span> Create<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> data, <span style="color: #FF0000;">object</span> model<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> CreateInternal<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">Mvc</span>.<span style="color: #0000FF;">ViewDataDictionary</span><span style="color: #000000;">&#40;</span>model<span style="color: #000000;">&#41;</span>, data<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">Mvc</span>.<span style="color: #0000FF;">ViewDataDictionary</span> Create<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> data<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> CreateInternal<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">Mvc</span>.<span style="color: #0000FF;">ViewDataDictionary</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, data<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">Mvc</span>.<span style="color: #0000FF;">ViewDataDictionary</span> CreateInternal<span style="color: #000000;">&#40;</span><span style="color: #000000;">System</span>.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">Mvc</span>.<span style="color: #0000FF;">ViewDataDictionary</span> dictionary, <span style="color: #FF0000;">object</span> data<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            AddPropertiesToViewData<span style="color: #000000;">&#40;</span>dictionary, data<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">return</span> dictionary<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> AddPropertiesToViewData<span style="color: #000000;">&#40;</span><span style="color: #000000;">System</span>.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">Mvc</span>.<span style="color: #0000FF;">ViewDataDictionary</span> dictionary, <span style="color: #FF0000;">object</span> data<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>data <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> return<span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Reflection</span>.<span style="color: #0000FF;">PropertyInfo</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> properties <span style="color: #008000;">=</span> data.<span style="color: #0000FF;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">GetProperties</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var property <span style="color: #0600FF;">in</span> properties<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                dictionary.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>property.<span style="color: #0000FF;">Name</span>, property.<span style="color: #0000FF;">GetValue</span><span style="color: #000000;">&#40;</span>data, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> SetPropertiesToViewDataValues<span style="color: #000000;">&#40;</span><span style="color: #000000;">System</span>.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">Mvc</span>.<span style="color: #0000FF;">ViewUserControl</span> viewUserControl<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var property <span style="color: #0600FF;">in</span> viewUserControl.<span style="color: #0000FF;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">GetProperties</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>viewUserControl.<span style="color: #0000FF;">ViewData</span><span style="color: #000000;">&#91;</span>property.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                    property.<span style="color: #0000FF;">SetValue</span><span style="color: #000000;">&#40;</span>viewUserControl, Convert.<span style="color: #0000FF;">ChangeType</span><span style="color: #000000;">&#40;</span>viewUserControl.<span style="color: #0000FF;">ViewData</span><span style="color: #000000;">&#91;</span>property.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#93;</span>, property.<span style="color: #0000FF;">PropertyType</span><span style="color: #000000;">&#41;</span>, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Hope that&#8217;s useful to you!</p>
]]></content:encoded>
			<wfw:commentRss>http://davidlaing.com/2008/11/27/aspnet-mvc-beta-setting-properties-on-viewcontrols/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adding a prompt option to Html.Select in ASP.NET MVC</title>
		<link>http://davidlaing.com/2008/05/05/adding-a-prompt-option-to-htmlselect-in-aspnet-mvc/</link>
		<comments>http://davidlaing.com/2008/05/05/adding-a-prompt-option-to-htmlselect-in-aspnet-mvc/#comments</comments>
		<pubDate>Mon, 05 May 2008 13:58:28 +0000</pubDate>
		<dc:creator>mrdavidlaing</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>

		<guid isPermaLink="false">http://davidlaing.com/?p=6</guid>
		<description><![CDATA[Digging around the source code to the ASP.NET MVC Source Refresh, I discovered a useful extension to the Html.Select helper method.
Say you want a select dropdown with a NULL case, which instructs the user to make a selection.
Eg: You want your first option to be:  ==Select==
To do so, call Html.Select with a htmlAttribute of prompt=&#8221;==Select==&#8221;
Like:
&#60;%=Html.Select((&#8220;Quote.ClientId&#8221;), [...]]]></description>
			<content:encoded><![CDATA[<p>Digging around the source code to the ASP.NET MVC Source Refresh, I discovered a useful extension to the Html.Select helper method.</p>
<p>Say you want a select dropdown with a NULL case, which instructs the user to make a selection.</p>
<p>Eg: You want your first option to be:  ==Select==</p>
<p>To do so, call Html.Select with a htmlAttribute of prompt=&#8221;==Select==&#8221;</p>
<p>Like:</p>
<p>&lt;%=Html.Select((&#8220;Quote.ClientId&#8221;), ViewData.Clients, &#8220;Name&#8221;,&#8221;Id&#8221;, ViewData.Quote.ClientId,0,false,new {prompt=&#8221;==Select==&#8221;})%&gt;</p>
]]></content:encoded>
			<wfw:commentRss>http://davidlaing.com/2008/05/05/adding-a-prompt-option-to-htmlselect-in-aspnet-mvc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
