<?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>In search of perfection... &#187; Strongly Typed</title>
	<atom:link href="http://agilefutures.com/index.php/tag/strongly-typed/feed/" rel="self" type="application/rss+xml" />
	<link>http://agilefutures.com</link>
	<description>Illegitimi Non Carborundum</description>
	<lastBuildDate>Sun, 23 Aug 2009 10:23:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>MVC – Strongly Typed Global ViewData</title>
		<link>http://agilefutures.com/index.php/2009/06/mvc-strongly-typed-global-viewdata/</link>
		<comments>http://agilefutures.com/index.php/2009/06/mvc-strongly-typed-global-viewdata/#comments</comments>
		<pubDate>Tue, 02 Jun 2009 12:25:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Strongly Typed]]></category>
		<category><![CDATA[ViewData]]></category>

		<guid isPermaLink="false">http://agilefutures.com/?p=66</guid>
		<description><![CDATA[Most MVC stuff I have been doing has been quite basic and the knowledge gleaned off many blog sites ( thanks to you all )I’m not a prolific blogger – too busy!!!, I’ll only blog when I have something to useful to share that isn’t easy to find. I was looking for a way to [...]]]></description>
			<content:encoded><![CDATA[<p>Most MVC stuff I have been doing has been quite basic and the knowledge gleaned off many blog sites ( thanks to you all <img src='http://agilefutures.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  )I’m not a prolific blogger – too busy!!!, I’ll only blog when I have something to useful to share that isn’t easy to find.    </p>
<p> I was looking for a way to provide ViewData at the Model level so that I could have some parameters available after every action that were strongly typed and injected into the ActionResult instead of ViewData[&quot;MyData&quot;]. I want to use Model.MyData</p>
<p>I had a good search about but nothing definitive so I cam up with this.    </p>
<p> The Base Controller Class.     </p>
<p> It’s always a good idea to have you own base controller that is inherited from your other controllers. Here we are using it for storing Global Settings/Properties or any helper methods etc.</p>

<div class="wp_syntax"><table><tr><td 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
54
55
56
57
58
59
60
61
62
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Web.Mvc</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> GlobalViewData.<span style="color: #0000FF;">Controllers</span>
<span style="color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Base data container for ViewData Models</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #0600FF;">public</span> abstract <span style="color: #FF0000;">class</span> BaseFormViewModel
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> MyData <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: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Base controller that all controllers inherit from.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #0600FF;">public</span> abstract <span style="color: #FF0000;">class</span> BaseController <span style="color: #008000;">:</span> Controller
    <span style="color: #000000;">&#123;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Initializes a new instance of the &lt;see cref=&quot;BaseController&quot; /&gt; class.</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #0600FF;">protected</span> BaseController<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            MyData <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;I have set this in the BaseController&quot;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> MyData <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: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Method called after the action method is invoked.</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;filterContext&quot; /&gt;Contains information about the current request and action.</span>
        <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnActionExecuted<span style="color: #000000;">&#40;</span>ActionExecutedContext filterContext<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// check there is data in the Model</span>
            <span style="color: #008080; font-style: italic;">//</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>filterContext.<span style="color: #0000FF;">Controller</span>.<span style="color: #0000FF;">ViewData</span>.<span style="color: #0000FF;">Model</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">// bit of reflection magic and get the types</span>
                <span style="color: #008080; font-style: italic;">//</span>
                Type _baseFormViewModelType <span style="color: #008000;">=</span> <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>BaseFormViewModel<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                Type _modelType <span style="color: #008000;">=</span> filterContext.<span style="color: #0000FF;">Controller</span>.<span style="color: #0000FF;">ViewData</span>.<span style="color: #0000FF;">Model</span>.<span style="color: #0000FF;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// if the model type is derived from the base type then</span>
                <span style="color: #008080; font-style: italic;">// we can set some properties.</span>
                <span style="color: #008080; font-style: italic;">//</span>
&nbsp;
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_baseFormViewModelType.<span style="color: #0000FF;">IsAssignableFrom</span><span style="color: #000000;">&#40;</span>_modelType<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    <span style="color: #008080; font-style: italic;">// inject the value to the ViewData.Model property</span>
                    <span style="color: #008080; font-style: italic;">//</span>
                    _modelType.<span style="color: #0000FF;">GetProperty</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;MyData&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">SetValue</span><span style="color: #000000;">&#40;</span>filterContext.<span style="color: #0000FF;">Controller</span>.<span style="color: #0000FF;">ViewData</span>.<span style="color: #0000FF;">Model</span>, MyData, <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: #0600FF;">base</span>.<span style="color: #0000FF;">OnActionExecuted</span><span style="color: #000000;">&#40;</span>filterContext<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#125;</span>       
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>The Home Controller Class</p>
<p>This inherits from the BaseController and also has a Data Class called HomeFormViewModel which inherits from BaseFormViewModel.</p>

<div class="wp_syntax"><table><tr><td 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
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Linq</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Web</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Web.Mvc</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> GlobalViewData.<span style="color: #0000FF;">Controllers</span>
<span style="color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> HomeFormViewModel <span style="color: #008000;">:</span> BaseFormViewModel
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> LocalData <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: #000000;">&#125;</span>
&nbsp;
    <span style="color: #000000;">&#91;</span>HandleError<span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> HomeController <span style="color: #008000;">:</span> BaseController
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> ActionResult Index<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            ViewData<span style="color: #000000;">&#91;</span><span style="color: #008000;">&amp;</span>quot<span style="color: #008000;">;</span>Message<span style="color: #008000;">&amp;</span>quot<span style="color: #008000;">;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Welcome to ASP.NET MVC!&quot;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">return</span> View<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> ActionResult About<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            var _formData <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> HomeFormViewModel <span style="color: #000000;">&#123;</span>LocalData <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;I have set this in the Home Controller&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">return</span> View<span style="color: #000000;">&#40;</span>_formData<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>&#160;</p>
<p>When the About Action is called the&#160; <strong>HomeFormViewModel</strong>&#160; is populated and then caught by the BaseController <strong>OnActionExecuted</strong> method which injects the required data into the HomeFormViewModel which is derived from <strong>BaseFormViewModel</strong></p>
<p>In About.aspx</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #006600; font-weight: bold;">&lt;</span> <span style="color: #006600; font-weight: bold;">%@</span> Page Language<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;C#&quot;</span> MasterPageFile<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;~/Views/Shared/Site.Master&quot;</span> Inherits<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;System.Web.Mvc.ViewPage&lt;HomeFormViewModel&gt;&quot;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>
&nbsp;
<span style="color: #006600; font-weight: bold;">&lt;</span> <span style="color: #006600; font-weight: bold;">%@</span> Import Namespace<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;GlobalViewData.Controllers&quot;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>
<span style="color: #006600; font-weight: bold;">&lt;</span>asp <span style="color: #006600; font-weight: bold;">:</span>Content ID<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;aboutTitle&quot;</span> ContentPlaceHolderID<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;TitleContent&quot;</span> runat<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;server&quot;</span><span style="color: #006600; font-weight: bold;">&gt;</span>
    About Us
<span style="color: #006600; font-weight: bold;">&lt;/</span>asp<span style="color: #006600; font-weight: bold;">&gt;</span>
<span style="color: #006600; font-weight: bold;">&lt;</span>asp <span style="color: #006600; font-weight: bold;">:</span>Content ID<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;aboutContent&quot;</span> ContentPlaceHolderID<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;MainContent&quot;</span> runat<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;server&quot;</span><span style="color: #006600; font-weight: bold;">&gt;</span>
    <span style="color: #006600; font-weight: bold;">&lt;</span>h2<span style="color: #006600; font-weight: bold;">&gt;</span>
        About<span style="color: #006600; font-weight: bold;">&lt;/</span>h2<span style="color: #006600; font-weight: bold;">&gt;</span>
    <span style="color: #006600; font-weight: bold;">&lt;</span>p<span style="color: #006600; font-weight: bold;">&gt;</span>
        <span style="color: #990099; font-weight: bold;">Global</span> - From BaseController
        <span style="color: #006600; font-weight: bold;">&lt;</span> <span style="color: #006600; font-weight: bold;">%=</span>Html.<span style="color: #9900cc;">Encode</span><span style="color: #006600; font-weight:bold;">&#40;</span>Model.<span style="color: #9900cc;">MyData</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>
    <span style="color: #006600; font-weight: bold;">&lt;/</span>p<span style="color: #006600; font-weight: bold;">&gt;</span>
    <span style="color: #006600; font-weight: bold;">&lt;</span>p<span style="color: #006600; font-weight: bold;">&gt;</span>
        Local - From HomeController
        <span style="color: #006600; font-weight: bold;">&lt;</span> <span style="color: #006600; font-weight: bold;">%=</span>Html.<span style="color: #9900cc;">Encode</span><span style="color: #006600; font-weight:bold;">&#40;</span>Model.<span style="color: #9900cc;">LocalData</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>
    <span style="color: #006600; font-weight: bold;">&lt;/</span>p<span style="color: #006600; font-weight: bold;">&gt;</span>
<span style="color: #006600; font-weight: bold;">&lt;/</span>asp<span style="color: #006600; font-weight: bold;">&gt;</span></pre></td></tr></table></div>

<p><strong></strong></p>
<p><strong>As you can see from the image below the data is populated.</strong></p>
<p><strong></strong></p>
<p><strong></strong></p>
<p><a href="http://agilefutures.com/wp-content/uploads/2009/06/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://agilefutures.com/wp-content/uploads/2009/06/image-thumb.png" width="244" height="113" /></a></p>
<p><strong>Hope this is useful!</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://agilefutures.com/index.php/2009/06/mvc-strongly-typed-global-viewdata/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

