<?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>Chris van de Steeg &#187; asp.net mvc</title>
	<atom:link href="http://www.chrisvandesteeg.nl/category/aspnet-mvc/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.chrisvandesteeg.nl</link>
	<description>What&#039;s practical is logical</description>
	<lastBuildDate>Thu, 06 May 2010 07:42:21 +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>ASP.NET MVC, DynamicData, Domain-/RiaServices, Unity and NHibernate: Part 1</title>
		<link>http://www.chrisvandesteeg.nl/2009/07/13/aspnet-mvc-dynamicdata-domain-riaservices-unity-and-nhibernate-part-1/</link>
		<comments>http://www.chrisvandesteeg.nl/2009/07/13/aspnet-mvc-dynamicdata-domain-riaservices-unity-and-nhibernate-part-1/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 13:28:13 +0000</pubDate>
		<dc:creator>Chris van de Steeg</dc:creator>
				<category><![CDATA[RiaServices]]></category>
		<category><![CDATA[asp.net mvc]]></category>
		<category><![CDATA[nhiberntate]]></category>
		<category><![CDATA[unity]]></category>

		<guid isPermaLink="false">http://www.chrisvandesteeg.nl/?p=91</guid>
		<description><![CDATA[I’ve been playing with RIAServices and DynamicData lately, and managed to combine these with NHibernate as ORM in an ASP.NET MVC project. For this project the following (extensive) list of libraries is used (all these libraries are included in the download): MS ASP.NET Mvc 1.0 Microsoft .NET RIA Services July 2009 Preview NHibernate 2.1.0CR1 NHibernate [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve been playing with RIAServices and DynamicData lately, and managed to combine these with NHibernate as ORM in an ASP.NET MVC project.</p>
<p>For this project the following (extensive) list of libraries is used (all these libraries are included in the download):</p>
<ul>
<li><a href="http://go.microsoft.com/fwlink/?LinkId=144444" target="_blank">MS ASP.NET Mvc 1.0</a> </li>
<li><a href="http://go.microsoft.com/fwlink/?LinkId=144609" target="_blank">Microsoft .NET RIA Services July 2009 Preview</a> </li>
<li><a href="http://nhforge.org/blogs/nhibernate/archive/2009/07/05/good-morning-nhibernate-2-1-0cr1.aspx" target="_blank">NHibernate 2.1.0CR1</a> </li>
<li><a href="http://nhcontrib.svn.sourceforge.net/viewvc/nhcontrib/trunk/src/NHibernate.Linq/" target="_blank">NHibernate Linq</a> (latest trunk version) </li>
<li><a href="http://fluentnhibernate.org/" target="_blank">FluentNhibernate</a> (latest trunk version, patched to work with NHibernate 2.1.0CR1) </li>
<li><a href="http://www.codeplex.com/unity" target="_blank">MS Unity 1.2</a> </li>
<li><a href="http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=18803" target="_blank">ASP.NET Dynamic Data MVC Preview</a> (since this project is pretty old, I had to modify it quite a bit, the source is included in the download) </li>
<li>I used <a href="http://blog.wekeroad.com/blog/make-visual-studio-generate-your-repository/" target="_blank">Rob Conery’s t4 templates</a> as the base to generate entities from the database. </li>
</ul>
<p>Besides using these libraries, I copied some code snippits from the <a href="http://www.codeplex.com/Kigg" target="_blank">Kigg</a> project, which has quite a nice codebase!</p>
<p>In the first part of this serie, I’ll show you how you can use these libraries along with my own code to scaffold the entire Northwind database in an mvc project. It’ll be totally hassle-free, I promise! It will generate classes from the database-schema and I will also show you how to override the default generated domainservices and repositories (yes, those are generated at runtime if you don’t specify your own!).</p>
<p>In the upcoming parts, I will add client-side validation and some ajax-love to the scaffold pages. In one of those parts I’ll also explain how my code works, and what it does. I’m also thinking about showing how to replace some parts of the library, since it’s very pluggable. I could for example replace the persistencelayer with Linq-to-Sql or replace the Depency part with StructureMap.</p>
<p>Although this sample shows you how to use an existing database, it would ofcourse be best if you could use the Model-first approach! Luckely nHibernate makes this very simple as well. If you create the entity files in your project and have them inherited from BaseEntity, all you have to do is add this line to your Application_Start:</p>
<p>new SchemaUpdate(IoC.Resolve&lt;Configuration&gt;).Execute(true, true);</p>
<p>Your database schema will be automatically updated if you change anything to your Model.</p>
<p>You can watch the screencast <a href="http://www.screentoaster.com/watch/stVUJXRExIR11XRF5YXFJbUVVc" target="_blank">here</a> and download the sample + dll’s <a href="http://code.google.com/p/webdotnet/downloads/list" target="_blank">here</a></p>
<p>&#160;</p>
<p> <object id='stVUJXRExIR11XRF5YXFJbUVVc' width='600' height='400' type='application/x-shockwave-flash' data='http://www.screentoaster.com/swf/STPlayer.swf'  codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0'><param name="movie" value="http://www.screentoaster.com/swf/STPlayer.swf" /><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="flashvars" value="video=stVUJXRExIR11XRF5YXFJbUVVc" /></object>
<p>For the next screencast, I’ll by a mic so I don’t have to subtitle it <img src='http://www.chrisvandesteeg.nl/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.chrisvandesteeg.nl/2009/07/13/aspnet-mvc-dynamicdata-domain-riaservices-unity-and-nhibernate-part-1/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>jQuery ajax with asp.net mvc preview 5</title>
		<link>http://www.chrisvandesteeg.nl/2008/09/16/jquery-ajax-with-aspnet-mvc-preview-5/</link>
		<comments>http://www.chrisvandesteeg.nl/2008/09/16/jquery-ajax-with-aspnet-mvc-preview-5/#comments</comments>
		<pubDate>Tue, 16 Sep 2008 18:48:57 +0000</pubDate>
		<dc:creator>Chris van de Steeg</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[asp.net mvc]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.chrisvandesteeg.nl/?p=64</guid>
		<description><![CDATA[Quite some had changed between mvc p4 and mvc p5, so it took some time too update my jquery4mvc project to preview 5. Be sure to check out scott&#8217;s post to read all the new stuff. He&#8217;ll be posting on the ajaxhelpers some time soon also.&#160; If you wish to skip the reading and directly [...]]]></description>
			<content:encoded><![CDATA[<p>Quite some had changed between mvc p4 and mvc p5, so it took some time too update my jquery4mvc project to preview 5. Be sure to check out <a href="http://weblogs.asp.net/scottgu/archive/2008/09/02/asp-net-mvc-preview-5-and-form-posting-scenarios.aspx">scott&#8217;s post</a> to read all the new stuff. He&#8217;ll be posting on the ajaxhelpers some time soon also.&#160; If you wish to skip the reading and directly download the stuff: it&#8217;s at <a href="http://www.codeplex.com/jquery4mvc/Release/ProjectReleases.aspx?ReleaseId=17401">codeplex</a>. </p>
<p>The javascript-only package includes 3 javascript files that allow you to use the default Microsoft AjaxHelper. You can just copy those files to your Content folder and include them in your head section, instead of the MicrosoftAjax.js and MicrosoftMvcAjax.js. All ajax extensions work the same as their Microsoft equivalence,&#160; using jQuery instead of MicrosoftAjax. As a little bonus, you&#8217;ll get back and forward button support for your ajax-links.</p>
<p><a href="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/09/image.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="92" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/09/image-thumb.png" width="644" border="0" /></a> </p>
<p>That&#8217;s it, go ahead and use the AjaxHelper functions like you&#8217;re used to. Though a few things have changed for the AjaxHelpers in preview 5, you could still use the introduction <a href="http://www.hanselman.com/blog/ASPNETMVCPreview4UsingAjaxAndAjaxForm.aspx">ASP.NET MVC Preview 4 &#8211; Using Ajax and Ajax.Form</a> by Scott Hanselman if you haven&#8217;t already checked out the AjaxHelper. Main thing that has changed is that you cannot use inline javascript in the AjaxOptions in preview 5, you should now specify a method name. You could also download the entire jQueryMvc project, it holds a sample website showing some of the ajax magic.</p>
<p>Talking about the entire project, it&#8217;s more than just the .js files! The intention of the project is to get you up and running very quickly when you want to create an mvc based ajax application. We use <a href="http://codeplex.com/unity">Unity</a> as our default IoC container.</p>
<p>I think it&#8217;s quite hard to explain exactly what the project does for you, it&#8217;s best really to just look at the sample website, but I will give a brief overview. First, we have a DefaultViewViewEngine. If you mark your controller with a [DefaultView] attribute like so:</p>
<p><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="55" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/09/image1.png" width="406" border="0" /> </p>
<p>If you call in example /Home/Index, by default the Index view will be searched for. If it isn&#8217;t found, the DefaultViewViewEngine will look for a template with the name specified (&quot;default&quot; in the above example). It doesn&#8217;t matter what viewengine you use for your views, it will work for all of them. So what can this do? Look at the structure of the views in the sample site:</p>
<p><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="261" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/09/image2.png" width="189" border="0" /> </p>
<p>As you can see, most of the the views only have an ascx file prefixed with an underscore. Since the views are not found when the actions Home/About, Home/SayHello, Home/Index are called upon, the DefaultViewViewEngine will open Shared/Default.aspx.    <br />This default page does not do much<img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="76" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/09/image3.png" width="879" border="0" /> </p>
<p>As you can see, it just uses the masterpage. But with only little imagination, you can see that you can easily create different default-pages for each controller. The masterpage however, does hold some extra magic:</p>
<p><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="55" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/09/image4.png" width="506" border="0" /></p>
<p>This special contentplaceholder, by default works like a normal placeholder. But, if it doesn&#8217;t get new content from it&#8217;s page, it will ask the ViewEngine(s) to open the view of the current action prefixed with an underscore. So, if you call Home/About, this will ask for Home/_About.&#160; <br />Can you see the magic? For all similar pages, you just have to create the ascx files (or other partials if you use a different viewengine). Those partial views will be pushed into the main page.</p>
<p>But wait, there&#8217;s more! If you make an ajax-request to a controller that has the [AjaxController] attribute, it will return only the partial view! Now, isn&#8217;t that great ? That looks a lot like the updatepanel to me <img src='http://www.chrisvandesteeg.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><a href="http://www.codeplex.com/jquery4mvc/Release/ProjectReleases.aspx?ReleaseId=17401">Go to codeplex and download the project to see the ajax-magic I&#8217;m so excited about!</a>    <br />Source can be downloaded at Google code: <a href="http://code.google.com/p/jquerymvc/source/checkout">http://code.google.com/p/jquerymvc/source/checkout</a>    <br />The sample website can be seen in action at: <a title="http://www.chrisvandesteeg.nl/demo/jquerymvc5/" href="http://www.chrisvandesteeg.nl/demo/jquerymvc5/">http://www.chrisvandesteeg.nl/demo/jquerymvc5/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.chrisvandesteeg.nl/2008/09/16/jquery-ajax-with-aspnet-mvc-preview-5/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Howto use jQuery with asp.net mvc preview 4&#8242;s AjaxHelper</title>
		<link>http://www.chrisvandesteeg.nl/2008/07/21/howto-use-jquery-with-aspnet-mvc-preview-4s-ajaxhelper/</link>
		<comments>http://www.chrisvandesteeg.nl/2008/07/21/howto-use-jquery-with-aspnet-mvc-preview-4s-ajaxhelper/#comments</comments>
		<pubDate>Mon, 21 Jul 2008 20:56:11 +0000</pubDate>
		<dc:creator>Chris van de Steeg</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[asp.net mvc]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.chrisvandesteeg.nl/2008/07/21/howto-use-jquery-with-aspnet-mvc-preview-4s-ajaxhelper/</guid>
		<description><![CDATA[Working on jQuery4mvc preview 4, one of the first things I ofcourse wanted to do was to replace the ASP.NET Ajax library that&#8217;s used by the AjaxHelper. Unfortunately the AjaxHelper itself is sealed, so atm we can&#8217;t (easily) change how the AjaxHelper generates its links. So, as an alternative, I created a javascript library that [...]]]></description>
			<content:encoded><![CDATA[<p>Working on jQuery4mvc preview 4, one of the first things I ofcourse wanted to do was to replace the ASP.NET Ajax library that&#8217;s used by the AjaxHelper.</p>
<p>Unfortunately the AjaxHelper itself is sealed, so atm we can&#8217;t (easily) change how the AjaxHelper generates its links. So, as an alternative, I created a javascript library that uses the same methodnames as the MicrosoftMvcAjax version.</p>
<p>Though at first I was upset that AjaxHelper wasn&#8217;t opened up, this javascript-only solution makes it very easy to use. All you have to do is </p>
<ul>
<li>place the <a href="http://jquery.com/">latest jQuery version</a> in your Content folder; </li>
<li>place <a href="http://www.codeplex.com/jquery4mvc/Release/ProjectReleases.aspx?ReleaseId=15521">my javascript file</a> in your Content folder; </li>
<li>add references in your views (or masterpage ofcourse) to these 2 files instead of referencing the MicrosoftMvcAjax.js and MicrosoftAjax.js file.</li>
</ul>
<p>If you haven&#8217;t played with the AjaxHelper yet, take a look at <a href="http://www.hanselman.com/blog/ASPNETMVCPreview4UsingAjaxAndAjaxForm.aspx">Scott Hanselman&#8217;s post on Using Ajax and Ajax.Form</a></p>
<p>This jQuery version also has a little extra: browser-history support for your ajax requests</p>
<p>Now, go <a href="http://www.codeplex.com/jquery4mvc/Release/ProjectReleases.aspx?ReleaseId=15521">download</a> the js file and play!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chrisvandesteeg.nl/2008/07/21/howto-use-jquery-with-aspnet-mvc-preview-4s-ajaxhelper/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>jQuery for Asp.net MVC preview 3</title>
		<link>http://www.chrisvandesteeg.nl/2008/06/13/jquery-for-aspnet-mvc-preview-3/</link>
		<comments>http://www.chrisvandesteeg.nl/2008/06/13/jquery-for-aspnet-mvc-preview-3/#comments</comments>
		<pubDate>Fri, 13 Jun 2008 13:13:08 +0000</pubDate>
		<dc:creator>Chris van de Steeg</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[asp.net mvc]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.chrisvandesteeg.nl/2008/06/13/jquery-for-aspnet-mvc-preview-3/</guid>
		<description><![CDATA[So, I finally got a chance to sit down and finish up the jQuery for Asp.net Mvc preview 3. If you haven&#8217;t already read Scott&#8217;s post about preview 3,&#160; you should definitely do so before reading this one. So, quite some things have changed in preview 3, which made me decide to change a lot [...]]]></description>
			<content:encoded><![CDATA[<p>So, I finally got a chance to sit down and finish up the jQuery for Asp.net Mvc preview 3. If you haven&#8217;t already read <a href="http://weblogs.asp.net/scottgu/archive/2008/05/27/asp-net-mvc-preview-3-release.aspx">Scott&#8217;s post about preview 3</a>,&#160; you should definitely do so before reading this one.</p>
<p>So, quite some things have changed in preview 3, which made me decide to change a lot in the jQuery for Asp.net mvc (<em>jqmvc</em>) too. To start with most radical one: I ditched the ajax.master. This little &#8216;framework&#8217; does not rely on a special masterpage in your website from now on. Since things have radically changed, I&#8217;ll start all over again on how to use this project. </p>
<p>In this post, I&#8217;ll explain how the project works globally. I won&#8217;t go into too much details about everything, but I will just explain the things you&#8217;ll directly touch in your application. In the next post, I&#8217;ll explain how to <em>use</em> the project in your website (until that time you could take a look at the included sample website). That post will explain the Ajax- and HtmlHelpers in this project and it will explain how to do an ajax request using jQuery.</p>
<p>Before my story starts, let&#8217;s start with the links and downloads for all those who want to read code rather than stories :</p>
<ul>
<li><a href="http://www.codeplex.com/jquery4mvc/Release/ProjectReleases.aspx?ReleaseId=14383">Downloads</a>&#160;</li>
<li><a href="http://code.google.com/p/jquerymvc/source/checkout">SVN source Repository</a></li>
<li><a href="http://code.google.com/p/jquerymvc/downloads/detail?name=preview%203%20bin.zip&amp;can=2&amp;q="></a><a href="http://www.chrisvandesteeg.nl/demo/jquerymvc3/default.aspx">The online demo</a> </li>
<li><a href="http://www.codeplex.com/jquery4mvc/">Codeplex homepage</a></li>
</ul>
<h2>Application and routes</h2>
<p>To have a quick go for your application, it&#8217;s recommended that you inherit your application from JqueryMvc.JqueryMvcApplication. You can ofcourse easily do this in your global.asax like so:    <br /><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="22" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/06/image3.png" width="531" border="0" />     <br />This application sets up the Unity dependency container and will set up the default routes. If you wish to setup a different (or no) dependency container, you should override InitializeContainer() and ConfigureContainer(). JQueryMvc itself is no longer dependent on a dependency container.</p>
<p>I found it hard to set up the default routes in preview 3 if your server does not support wildcard redirects (like visual studio&#8217;s webdevserver doesn&#8217;t). In that case you need to catch existing files (default.aspx needs to exist, but shouldn&#8217;t do anything), but you need to skip the Content folder. I came up with the following solution:    <br /><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="648" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/06/image6.png" width="536" border="0" />     <br />So if there&#8217;s a default.aspx in the root of your application, it set&#8217;s routeExistingFiles to true. If the url matches the RegEx ^Content\/.* the routing will be stopped using the new IgnoreRoute method. Also, your controllers will then be extended with .aspx to be sure the requests passes the asp.net cycle. If you have the option to catch all incoming requests, all you have to do is remove the default.aspx from the root of your website.</p>
<h1></h1>
<h2>Controllers</h2>
<p>As you&#8217;ve seen in Scott&#8217;s post, controllers should now return an ActionResult. The normal way would be to have your controller action return the result of the View() method. With <em>jqmvc</em> you should not call the View() method at the end of your action. Ofcourse, if you&#8217;re sure you want to return the normal view, you could still do so. In <em>jqmvc</em> there is a ExtController which you should inherit your controllers from. This baseclass contains a method called Action() which accepts the same attributes as View() but does some magic to decide what type of request the current request is, and will try to return the correct ActionResult for that type of request. This could be either an Ajax-request, a Json request or a normal page-request. Your typical controller with some actions would look like this:</p>
<p>&#160;&#160; <img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="399" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/06/image7.png" width="537" border="0" /></p>
<h2>Views</h2>
<p>If <em>jqmvc</em> encountered the current request as a normal page request, it will create the default ViewResult, nothing special there.</p>
<p>If <em>jqmvc </em>decided Json should be returned, a JsonResult is created and no further actions will be taken. The rendered view will be the ViewData serialized to Json. The ViewEngine of the controller is also changed to a JsonViewEngine but this is only called upon in case of an Exception.</p>
<p>If <em>jqmvc</em> decided that the request is an Ajax request, it will create a normal ViewResult, but it will change the ViewLocator of the controller.     <br />(Offtopic: I think it&#8217;s a design-error to have the viewlocator on the controller. It should be on the ViewResult object, that&#8217;s the only object that uses it. Perhaps this will be changed in the next preview&#8230;)     <br />This AjaxViewLocator will first look for .ascx files instead of .aspx files. If it can&#8217;t find the .ascx it will load .aspx afterall.&#160; You can also create a target-specific .ascx file: if you do an ajax-request through our javascript function jQuery.mvc.request() (or one of the htmlhelper functions), you have to specify a target for the loaded content. Let&#8217;s say we want to load /Home/Index into a &lt;div id=&quot;dynamiccontent&quot;&gt;&lt;/div&gt;     <br />The AjaxViewLocator will then look for :</p>
<ol>
<li>/Views/Home/Index.dynamiccontent.ascx </li>
<li>/Views/Home/Index.acx </li>
<li>/Views/Home/dynamiccontent.ascx </li>
<li>/Views/Home/Index.aspx </li>
<li>/Views/Shared/Index.dynamiccontent.ascx </li>
<li>/Views/Shared/Index.ascx </li>
<li>/Views/Shared/dynamiccontent.ascx </li>
<li>/Views/Shared/Index.aspx </li>
</ol>
<p>To have the page /Home/index available both as an ajax request as well as normal request, you could create an /Views/Home/Index.aspx which loads the index.ascx like so:<img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="25" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/06/image1.png" width="454" border="0" /> </p>
<h2>Things to know</h2>
<h4></h4>
<ul>
<li>
<p>We have some HelperClasses to simplify the use of <em>jqmvc</em></p>
</li>
<li>
<p>The ViewData keys &quot;Messages&quot; and &quot;Errors&quot; are treated specially by <em>jqmvc.</em> </p>
</li>
<li>
<p>Your page should always contain:</p>
<ul>
<li>
<p>&lt;%= Html.RegisterJqueryMvc(Page.ClientScript) %&gt; To register the required javascript files</p>
</li>
<li>
<p>&lt;div id=&quot;loading&quot;&gt;&lt;/div&gt;; or something else with id=&quot;loading&quot;. This will be made visible during ajax-requests</p>
</li>
<li>
<p>&lt;div id=&quot;errors&quot;&gt;&lt;/div&gt; to display errors</p>
</li>
<li>
<p>&lt;div id=&quot;messages&quot;&gt;&lt;/div&gt; to display messages (informational)</p>
</li>
</ul>
</li>
<li>
<p>We have a special contentplaceholder in JqueryMvc.UI.WebControls.ContentPlaceHolder. This will use the AjaxViewLocator to load some default content. Eg, if used like this in your masterpage, and the current request is /Home/Index:        <br />&lt;jq:ContentPlaceHolder ID=&quot;maincontent&quot; runat=&quot;server&quot; /&gt;         <br />It will look for for content in:</p>
<ol>       </ol>
<ol>
<li>/Views/Home/Index.maincontent.ascx </li>
<li>/Views/Home/Index.acx </li>
<li>/Views/Home/maincontent.ascx </li>
<li>/Views/Home/Index.aspx </li>
<li>/Views/Shared/Index.maincontent.ascx </li>
<li>/Views/Shared/Index.ascx </li>
<li>/Views/Shared/maincontent.ascx </li>
<li>/Views/Shared/Index.aspx
<p>This way, your index.aspx could just be an empty page that points to MasterPage with this default-content-loading contentplaceholer.<img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="27" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/06/image2.png" width="454" border="0" /> </li>
</ol>
</li>
<li>Your ascx files should inherit from JqueryMvc.Mvc.ExtViewUserControl to have access to the AjaxHelper (bug in asp.net mvc!) and to have the messages &amp; errors parsed by <em>jqmvc</em>. </li>
<li>In case of an error the default controller will look for a rescue page in /Views/[ControllerName]/Rescues/error.aspx or /Views/Shared/Rescues/error.aspx (or .ascx in case of an ajax request). You can specify your own rescues for different types of errors by using the RescueAttribute of </li>
<li>You should remove default.aspx if your application-server supports wildcard mappings. </li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.chrisvandesteeg.nl/2008/06/13/jquery-for-aspnet-mvc-preview-3/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>jQuery for ASP.NET MVC preview 2</title>
		<link>http://www.chrisvandesteeg.nl/2008/03/26/jquery-for-aspnet-mvc-preview-2/</link>
		<comments>http://www.chrisvandesteeg.nl/2008/03/26/jquery-for-aspnet-mvc-preview-2/#comments</comments>
		<pubDate>Wed, 26 Mar 2008 19:37:22 +0000</pubDate>
		<dc:creator>Chris van de Steeg</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[asp.net mvc]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.chrisvandesteeg.nl/2008/03/26/jquery-for-aspnet-mvc-preview-2/</guid>
		<description><![CDATA[I updated the jQueryMvc project so that it works with ASP.NET MVC preview 2. I also switched the IoC container from Objectbuilder to Unity. I submitted the Unity container to the MvcContrib project but it has not yet been accepted in their repository, so you&#8217;ll have to do with the unofficial version I include in [...]]]></description>
			<content:encoded><![CDATA[<p>I updated the jQueryMvc project so that it works with <a href="http://www.google.nl/url?sa=t&amp;ct=res&amp;cd=1&amp;url=http%3A%2F%2Fwww.microsoft.com%2Fdownloads%2Fdetails.aspx%3FFamilyID%3D38cc4cf1-773a-47e1-8125-ba3369bf54a3&amp;ei=t57qR77FIJacxAHfgu31Cg&amp;usg=AFQjCNH5Y3w0Qt8KMrr3vLRHHQH1GwEJpQ&amp;sig2=_BtORUlMGY88ZK2Qqjz46g">ASP.NET MVC preview 2</a>. I also switched the IoC container from Objectbuilder to <a href="http://www.codeplex.com/unity">Unity</a>. I submitted the Unity container to the <a href="http://www.mvccontrib.org">MvcContrib</a> project but it has not yet been accepted in their repository, so you&#8217;ll have to do with the unofficial version I include in the download. Unity is, to put it very baldly, Objectbuilder version 2. So it seemed logical to make the switch.</p>
<p>I also made up a nice(r) way to inject the ViewEngine into the BaseController. It&#8217;s now injected through a public method using a MethodInjection attribute:</p>
<p><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="137" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/03/image.png" width="450" border="0"/></p>
<p>Thanks to this method, we no longer have pre-register all the Controllers with our container (in the previous version of jQueryMvc I scanned all dll&#8217;s on ApplicationStart to register the Controller and assign the ServiceViewFactory as the default ViewFactory)</p>
<p>Furthermore, since the ASP.NET MVC team decided to change the way the Views are rendered (now through a ViewEngine, which is nice I think), it was no longer needed to use the code from <a href="http://www.aaronlerch.com/blog/2008/01/01/unifying-web-sites-and-web-services-with-the-aspnet-mvc-framework/">Aaron Lerch</a>. Though still inspired by his code ofcourse, I removed almost all of his code now. We now have a single &#8216;<a href="http://jquerymvc.googlecode.com/svn/trunk/jQueryMvc/ServiceViewEngine.cs">ServiceViewEngine</a>&#8216; inheriting from the ASP.NET MVC&#8217;s default WebFormViewEngine. All code deciding what view to render, is now located in this class.</p>
<p>What I did not like about preview 2, is that the name of the master-template cannot be set from within the ViewEngine. It&#8217;s now stored into the ViewContext, and the setter is private <img src='http://www.chrisvandesteeg.nl/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> . So now the mastertemplate is set in the BaseController, causing the BaseController to have to know what type of request the current one is (which is bad):</p>
<p><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="191" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/03/image1.png" width="577" border="0"/> </p>
<p>As usual, you can download the hottest source from the Subversion repository at <a title="http://code.google.com/p/jquerymvc/source/checkout" href="http://code.google.com/p/jquerymvc/source/checkout">http://code.google.com/p/jquerymvc/source/checkout</a> or you can <a href="/download/jquerymvc/jquerymvc-0.3.zip">download a zipped package of the source code</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chrisvandesteeg.nl/2008/03/26/jquery-for-aspnet-mvc-preview-2/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>jQuery for ASP.NET Mvc &#8211; part 2</title>
		<link>http://www.chrisvandesteeg.nl/2008/02/16/jquery-for-aspnet-mvc-part-2/</link>
		<comments>http://www.chrisvandesteeg.nl/2008/02/16/jquery-for-aspnet-mvc-part-2/#comments</comments>
		<pubDate>Sat, 16 Feb 2008 20:35:59 +0000</pubDate>
		<dc:creator>Chris van de Steeg</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[asp.net mvc]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.chrisvandesteeg.nl/2008/02/16/jquery-for-aspnet-mvc-part-2/</guid>
		<description><![CDATA[[go to downloads] A view days after I created my sample project that added some jQuery magic to the ASP.NET Mvc Framework, Aaron Lerch wrote an interesting article. He used a ViewFactory to do the json-serialization and added XML serialization as well. I liked this approach better then using masterpages. I took his code, altered [...]]]></description>
			<content:encoded><![CDATA[<p><a href="#downloads">[go to downloads]</a></p>
<p>A view days after I created my sample project that added some jQuery magic to the ASP.NET Mvc Framework, <a href="http://www.aaronlerch.com/">Aaron Lerch</a> wrote an <a href="http://www.aaronlerch.com/blog/2008/01/01/unifying-web-sites-and-web-services-with-the-aspnet-mvc-framework/">interesting article</a>.</p>
<p>He used a ViewFactory to do the json-serialization and added XML serialization as well. I liked this approach better then using masterpages. I took his code, altered some bits and created a library called jQueryMvc with it. This library also utilizes the <a href="http://www.codeplex.com/MVCContrib">MvcContrib</a> project. It&#8217;s now even easier to make your ASP.NET Mvc project ajax-enabled with jQuery, it just requires 4 steps:</p>
<ol>
<li>Add a reference to jQueryMvc.dll </li>
<li>Make your global.asax inherit from JqueryMvcApplication </li>
<li>Add &lt;%= Ajax.RegisterJqueryMvc(Page.ClientScript) %&gt; to the &lt;head&gt; section of your (master)pages. </li>
<li>Let your controllers inherit from BaseController </li>
</ol>
<p>That&#8217;s it, your all set. You should take a look at the SampleSite that&#8217;s included in the download on how to use the utility. Using it works almost the same as the previous version, so if you haven&#8217;t done so, you should read my <a href="http://www.chrisvandesteeg.nl/2007/12/19/ajax-and-json-for-aspnet-mvc-with-jquery/">previous article</a>. The only difference is the exception handling. Since I decided to embrace the MvcContrib project, the exception handling for ajax &amp; html requests have changed. Exceptions are now handled with a [Rescue] attribute. The BaseController has a default Rescue attribute attached which leads all exceptions to the &quot;error.aspx&quot; rescue handler. So your project should at least have /Views/Shared/Rescues/error.aspx that outputs the exceptions for your site. If you want to catch specific exception for yourself, you can add a rescue attribute to your site that catches that error type. E.g. if you want to catch a SecurityException, you could add<a href="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/02/image.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="44" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/02/image-thumb.png" width="509" border="0" /></a> </p>
<p>If an exception of type SecurityException is thrown during a controller action execution in your HomeController, MvcContrib will look for /Views/Home/Rescues/security.aspx and if that doesn&#8217;t exist, it will look for /Views/Shared/Rescues/security.aspx. The viewdata for those pages will be the exception itself. The rescue attribute can also be used on method level.</p>
<p>Another nice thing is that the MvcContrib is build to use Inversion of Control (IoC). <strong>BUT!</strong>: if you don&#8217;t want to do anything with IoC for now, you can leave it where it is! jQueryMvc just uses it for attaching the ViewFactory to your controllers, that&#8217;s all you need to know. You can also stop reading just here, the rest is about the dependency container that I used.</p>
<p>For jQueryMvc I choose the <a href="http://www.codeplex.com/ObjectBuilder">Microsoft Objectbuilder</a> framework for this, although, with MvcContrib, you&#8217;re free to choose an other framework. I choose the ObjectBuilder, since I&#8217;m the one that committed that to MvcContrib <img src='http://www.chrisvandesteeg.nl/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>In JqueryMvcApplication, when the application get&#8217;s initialized, we&#8217;re looking for all controllers in the currently running project. <a href="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/02/image1.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="292" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/02/image-thumb1.png" width="756" border="0" /></a></p>
<p>All these controllers are registered with the Dependency container and our custom ViewFactory is also registered with this container. The ObjectBulderControllerFactory makes sure the controllers are fetched through that very container, and when that happens, the ViewFactory is attached to them.</p>
<p><a href="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/02/image2.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="108" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2008/02/image-thumb2.png" width="723" border="0" /></a></p>
<p>There are also some&#160; nice helper functions in JqueryMvcApplication, for making the registration of your own services/helpers/etc into the container a breeze.</p>
<p>I&#8217;ll talk about using the container in some other article sometime.</p>
<p>For now, have fun with jQueryMvc.</p>
<p> <a name="downloads"></a>
<p><strong>Downloads</strong></p>
<ul>
<li><a href="http://code.google.com/p/jquerymvc/">Project at GoogleCode</a> </li>
<li><a href="/download/jquerymvc/jquerymvc2.zip">Download source, binaries &amp; sample project</a> </li>
<li><a href="/demo/jquerymvc2/">View online demo of the sample site</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.chrisvandesteeg.nl/2008/02/16/jquery-for-aspnet-mvc-part-2/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Ajax and json for ASP.NET MVC with jQuery</title>
		<link>http://www.chrisvandesteeg.nl/2007/12/19/ajax-and-json-for-aspnet-mvc-with-jquery/</link>
		<comments>http://www.chrisvandesteeg.nl/2007/12/19/ajax-and-json-for-aspnet-mvc-with-jquery/#comments</comments>
		<pubDate>Wed, 19 Dec 2007 17:28:39 +0000</pubDate>
		<dc:creator>Chris van de Steeg</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[asp.net mvc]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.chrisvandesteeg.nl/2007/12/19/ajax-and-json-for-aspnet-mvc-with-jquery/</guid>
		<description><![CDATA[I&#8217;ve created an example project that uses the jQuery library to create an ajax enabled ASP.NET MVC website. Generally what this project does: Load views through ajax (with back-button support) Do ajax-style form-posts and retrieve only the messages, errors and updated content Do json form-posts and retrieve the errors and messages as a json object [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve created an example project that uses the <a href="http://jquery.com">jQuery</a> library to create an ajax enabled <a href="http://weblogs.asp.net/scottgu/archive/2007/12/09/asp-net-3-5-extensions-ctp-preview-released.aspx">ASP.NET MVC</a> website. Generally what this project does:</p>
<ul>
<li>Load views through ajax (with back-button support) </li>
<li>Do ajax-style form-posts and retrieve only the messages, errors and updated content </li>
<li>Do json form-posts and retrieve the errors and messages as a json object </li>
</ul>
<p>Here are the quicklinks for all you people who don&#8217;t want to read the story.</p>
<ul>
<li><a href="/demo/jquerymvc/">View online demo</a> </li>
<li><a href="/download/jquerymvc/jquerymvc.zip">Download webproject</a> </li>
<li><a href="/download/jquerymvc/jquerymvc.vsi">Download Visual Studio template</a> </li>
</ul>
<p>For all you people who want read about it before playing around, here&#8217;s a little intro to the code&#8230;</p>
<p>The project contains a BaseController, which should be inherited by all your controllers. This controller (ofcourse) overrides the System.Web.Mvc.Controller and handles the detection of Ajax requests and/or Json requests. It does that by overriding the Execute method and reading some http-headers. Unfortunately reading http-headers is the only (evil) way I know to detect this.    <br />Fortunately, while I was playing with this project, I saw that <a href="http://weblogs.asp.net/mschwarz/archive/2007/12/19/mvc-web-application-and-json.aspx">Michael Schwarz</a> posted exactly (well, 99%) the same solution as I figured out, so for now I&#8217;ll take this as the best solution until someone provides me a better one.</p>
<p><a href="http://www.chrisvandesteeg.nl/wp-content/uploads/2007/12/image3.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="748" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2007/12/image-thumb2.png" width="536" border="0" /></a></p>
<p>As you can see, there&#8217;s also some error handling in here. If there is an exception thrown during the controller execution, it&#8217;s wrapped in a simple class called SimpleException. This class is easily serializable, so it can be passed on as a javascript object to the browser, in case of a json request.&#160; In case of a non-json request (either normal or ajax), the default error handler called &quot;error.aspx&quot; in views/shared/ is rendered.</p>
<p>The BaseController also overrides the RenderView. In the RenderView, we do some special stuff if the request is a json- or ajax-request.</p>
<p><a href="http://www.chrisvandesteeg.nl/wp-content/uploads/2007/12/image4.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="536" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2007/12/image-thumb3.png" width="537" border="0" /></a></p>
<p>If the current request is a json-request, the template &quot;json.aspx&quot; is called upon. The default &quot;json.aspx&quot; is located at /views/shared/json.aspx and the only thing it does is serializing the current ViewData.</p>
<p><a href="http://www.chrisvandesteeg.nl/wp-content/uploads/2007/12/image5.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="110" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2007/12/image-thumb4.png" width="434" border="0" /></a></p>
<p>One special note is that in the codebehind, I had &#8216;strong type&#8217; the ViewData to an &#8216;object&#8217;, since the ViewData class doesn&#8217;t allow us to serialize the real object.</p>
<p>Then there are the 2 masterpages: ajax.master and site.master. Both of them contain 4 ContentPlaceHolders:</p>
<ul>
<li>Errors </li>
<li>Messages </li>
<li>maincontent </li>
<li>dynamiccontent </li>
</ul>
<p>Errors and messages should ofcourse output the errors and informational messages for the current request. I created a handy extension method on the HtmlHelper to quickly output these:</p>
<p><a href="http://www.chrisvandesteeg.nl/wp-content/uploads/2007/12/image6.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="171" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2007/12/image-thumb5.png" width="494" border="0" /></a></p>
<p>The maincontent should contain the entire page without the usual MasterPage stuff (eg navigation). The dynamiccontent should only contain the content that can change during the same request (eg. the &#8216;hello {name}&#8217; part in the <a href="/demo/jquerymvc/">demo</a>).</p>
<p>In case of an ajax request, the javascript adds a querystring to the request telling what content is requested (either maincontent or dynamiccontent) and the ajax.master decides what to output for that request based on this querystring:</p>
<p><a href="http://www.chrisvandesteeg.nl/wp-content/uploads/2007/12/image7.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="314" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2007/12/image-thumb6.png" width="536" border="0" /></a></p>
<p>Ajax links are very easily created, also through an extension method:</p>
<p><a href="http://www.chrisvandesteeg.nl/wp-content/uploads/2007/12/image8.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="58" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2007/12/image-thumb7.png" width="536" border="0" /></a></p>
<p>The first parameter is the text of the link, the second one is the action on the controller and the third one is the id of the container-div.</p>
<p>The other client functions are all form related, you an see them in use in the /views/home/index.aspx template:</p>
<p><a href="http://www.chrisvandesteeg.nl/wp-content/uploads/2007/12/image9.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="203" alt="image" src="http://www.chrisvandesteeg.nl/wp-content/uploads/2007/12/image-thumb8.png" width="534" border="0" /></a></p>
<p>You can use any of these methods to submit your forms, just use them as you like it. The methods can be found in /Content/mvc.js</p>
<p>Well that should be enough talking about code, just go ahead and play to find out the rest by yourself.</p>
<li><a href="/demo/jquerymvc/">View online demo</a> </li>
<li><a href="/download/jquerymvc/jquerymvc.zip">Download webproject</a> </li>
<li><a href="/download/jquerymvc/jquerymvc.vsi">Download Visual Studio template</a> </li>
]]></content:encoded>
			<wfw:commentRss>http://www.chrisvandesteeg.nl/2007/12/19/ajax-and-json-for-aspnet-mvc-with-jquery/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>asp.net mvc -&gt; the rise of the messy controllers</title>
		<link>http://www.chrisvandesteeg.nl/2007/12/14/aspnet-mvc-the-rise-of-the-messy-controllers/</link>
		<comments>http://www.chrisvandesteeg.nl/2007/12/14/aspnet-mvc-the-rise-of-the-messy-controllers/#comments</comments>
		<pubDate>Fri, 14 Dec 2007 08:40:24 +0000</pubDate>
		<dc:creator>Chris van de Steeg</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[asp.net mvc]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.chrisvandesteeg.nl/2007/12/14/aspnet-mvc-the-rise-of-the-messy-controllers/</guid>
		<description><![CDATA[All the buzz these days goes to asp.net mvc and as many others, I&#8217;m loving it! Messing around with it, and looking at samples of others, I see the controllers tend to get quite messy. Where normally the codebehinds got messy, it&#8217;s now moved to the controllers. Let&#8217;s say we&#8217;re using Subsonic as our data-access [...]]]></description>
			<content:encoded><![CDATA[<p>All the buzz these days goes to <a href="http://weblogs.asp.net/scottgu/archive/2007/12/09/asp-net-3-5-extensions-ctp-preview-released.aspx">asp.net mvc</a> and as many others, I&#8217;m loving it!</p>
<p>Messing around with it, and looking at samples of others, I see the controllers tend to get quite messy. Where normally the codebehinds got messy, it&#8217;s now moved to the controllers.</p>
<p>Let&#8217;s say we&#8217;re using <a href="http://www.subsonicproject.com">Subsonic<br />
</a> as our data-access layer and we have created this controlleraction:</p>
<pre name="code" class="c-sharp">
[ControllerAction]
public void List(string foldername)
{
    ICollection folders = GetFolders(userId);
    bool hasInbox = false, hasDrafts = false, hasSent = false, hasBin = false;
    foreach (PrivateMessageFolder folder in folders)
    {
        hasInbox = hasInbox || folder.FolderType == PrivateMessageFolderType.Inbox;
        hasDrafts = hasDrafts || folder.FolderType == PrivateMessageFolderType.Drafts;
        hasSent =hasSent || folder.FolderType == PrivateMessageFolderType.Sent;
        hasBin = hasBin|| folder.FolderType == PrivateMessageFolderType.Bin;
        if (hasInbox &#038;&#038; hasDrafts &#038;&#038; hasSent &#038;&#038; hasBin
            ) break;
    }
    if (!hasInbox)
    {
        folders.Add(CreateSystemFolder(PrivateMessageFolderType.Inbox, userId));
    }
    if (!hasDrafts)
    {
        folders.Add(CreateSystemFolder(PrivateMessageFolderType.Drafts, userId));
    }
    if (!hasSent)
    {
        folders.Add(CreateSystemFolder(PrivateMessageFolderType.Sent, userId));
    }
    if (!hasBin)
    {
        folders.Add(CreateSystemFolder(PrivateMessageFolderType.Bin, userId));
    }
    PrivateMessageCollection messages = new PrivateMessageCollection();
    messages.LoadAndCloseReader(SPs.GetMessages(foldername, UserId).GetReader());
    PMData data = new PMData
    {
        Folders = folders,
        PrivateMessages = messages
    };
    RenderView("List", data);
}
</pre>
<p>Now, while this works perfectly fine, it&#8217;s messy. It&#8217;s messy because now my controller is very dependant on how subsonic works. So if subsonic changes in the future, or I choose to pick another dataaccess layer since subsonic support has stopped(or whatever reason), I have to rewrite all my controllers! Not nice, not nice at all.<br />
It&#8217;s also messy because I can&#8217;t see at one glance what my controller is passing to my view. I have to actually understand the code to see what it&#8217;s passing on.</p>
<p>Now look at this peice of code:</p>
<pre name="code" class="c-sharp">
Daos.PrivateMessageDao privateMessageDao = new Forums.Daos.PrivateMessageDao();

[ControllerAction]
public void List(string foldername)
{
    //load the collection
    PMData data = new PMData
    {
        Folders = privateMessageDao.GetAndEnsureFolders(UserId),
        PrivateMessages = privateMessageDao.GetMessages(foldername, UserId)
    };
    RenderView("List", data);
}
</pre>
<p>All code that has been removed, is now in the privatemessagedao</p>
<p>To me, its quite clear what this is passing on, it&#8217;s very clean code. I moved all the data-access parts to the Dao. So the dao (or service, or what ever you want to name it) contains the logic that&#8217;s needed to retreive, update, delete, etc. The controller shouldn&#8217;t even care how that dao does that, it just needs the <a href="http://en.wikipedia.org/wiki/POCO">POCO</a>&#8216;s. </p>
<p>In our example, we also have folders to be created by default. This is by no means the responsibility of the controller, the controller should just tell something else that it needs those folders, that&#8217;s it. The other thingy should create them.</p>
<p>Now we have seperation of concerns, so we have the possibility of using Dependency Injection for accessing the dao (and others), but I&#8217;ll write about that some other time</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chrisvandesteeg.nl/2007/12/14/aspnet-mvc-the-rise-of-the-messy-controllers/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
