<?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>Crafting software&#187; ASP.NET MVC</title>
	<atom:link href="http://davidlaing.com/tag/aspnet-mvc/feed/" rel="self" type="application/rss+xml" />
	<link>http://davidlaing.com</link>
	<description>David Laing&#039;s thoughts on software development</description>
	<lastBuildDate>Wed, 01 Feb 2012 11:02:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<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/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=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: Which sets properties on a ViewUserControl [...]]]></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/">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>
<pre>
</pre>
<p>Which sets properties on a ViewUserControl like this:</p>
<pre>     public partial class YUIDataTable : ViewUserControl
    {
        public string ConfigNamespace { get; set; }
        public string DataTableId { get; set; }
        public bool HideFilter { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            ViewDataDictionaryBuilder.SetPropertiesToViewDataValues(this);
        }
    }</pre>
<p>Here is the full helper code.</p>
<pre>using System;

namespace MvcHelpers
{
    ///
    /// With thanks to http://www.vitaminzproductions.com/technology-blog/index.php/2008/11/12/setting-properties-using-aspnet-mvc/
    ///
    public static class ViewDataDictionaryBuilder
    {
        public static System.Web.Mvc.ViewDataDictionary Create(object data, ModelType model) where ModelType : class
        {
            return (System.Web.Mvc.ViewDataDictionary)CreateInternal(new System.Web.Mvc.ViewDataDictionary(model), data);
        }

        public static System.Web.Mvc.ViewDataDictionary Create(object data, object model)
        {
            return CreateInternal(new System.Web.Mvc.ViewDataDictionary(model), data);
        }

        public static System.Web.Mvc.ViewDataDictionary Create(object data)
        {
            return CreateInternal(new System.Web.Mvc.ViewDataDictionary(), data);
        }

        private static System.Web.Mvc.ViewDataDictionary CreateInternal(System.Web.Mvc.ViewDataDictionary dictionary, object data)
        {
            AddPropertiesToViewData(dictionary, data);
            return dictionary;
        }

        private static void AddPropertiesToViewData(System.Web.Mvc.ViewDataDictionary dictionary, object data)
        {
            if (data == null) return;

            System.Reflection.PropertyInfo[] properties = data.GetType().GetProperties();

            foreach (var property in properties)
            {
                dictionary.Add(property.Name, property.GetValue(data, null));
            }
        }

        public static void SetPropertiesToViewDataValues(System.Web.Mvc.ViewUserControl viewUserControl)
        {
            foreach (var property in viewUserControl.GetType().GetProperties())
            {
                if (viewUserControl.ViewData[property.Name] != null)
                    property.SetValue(viewUserControl, Convert.ChangeType(viewUserControl.ViewData[property.Name], property.PropertyType), null);
            }
        }

    }
}</pre>
<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>
	</channel>
</rss>

