<?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>devermind.com &#187; mvc</title>
	<atom:link href="http://devermind.com/tag/mvc/feed/" rel="self" type="application/rss+xml" />
	<link>http://devermind.com</link>
	<description>Adrian Grigore's software development weblog. Motto: I will not waste my time looking for a clever motto.</description>
	<lastBuildDate>Fri, 03 Sep 2010 13:25:22 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>ASP.NET MVC Tip #1: Using Custom ViewModels with POST action methods</title>
		<link>http://devermind.com/linq/aspnet-mvc-using-custom-viewmodels-with-post-action-methods/</link>
		<comments>http://devermind.com/linq/aspnet-mvc-using-custom-viewmodels-with-post-action-methods/#comments</comments>
		<pubDate>Sat, 18 Apr 2009 11:44:27 +0000</pubDate>
		<dc:creator>Adrian Grigore</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[binding]]></category>
		<category><![CDATA[custom]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[POST]]></category>
		<category><![CDATA[viewmodel]]></category>

		<guid isPermaLink="false">http://devermind.com/?p=102</guid>
		<description><![CDATA[One of the top good practices for ASP.NET MVC is not to use the ViewData Dictionary, but to put your data in a strongly typed ViewModel instead. Many people seem to be using Linq to SQL entities as a ViewModel, because it&#8217;s a very comfortable approach. But what do you do if your view should [...]]]></description>
			<content:encoded><![CDATA[<p>One of the top good practices for ASP.NET MVC is not to use the ViewData Dictionary, but to put your data in a strongly typed ViewModel instead. Many people seem to be using Linq to SQL entities as a ViewModel, because it&#8217;s a very comfortable approach. But what do you do if your view should contain data that is not included in any of your linq entities? <a href="http://weblogs.asp.net/scottgu/archive/2009/03/10/free-asp-net-mvc-ebook-tutorial.aspx">Scott Gu&#8217;s chapter 1 preview of his upcoming ASP.NET MVC book</a> recommends using a custom-shaped ViewModel for those cases.<span id="more-102"></span></p>
<p>One thing that still puzzled me after reading and trying to follow the chapter was how to create complex custom ViewModels (as opposed to putting all data in the ViewData dictionary or using vanilla Linq to SQL entities). Scott mentions on page 107 of the book  that you &#8220;might have the action method update a ViewModel object with the form-posted data, and then use the ViewModel instance to map or retrieve an actual domain model object&#8221;, but there is no actual example of how this would look in source code.</p>
<p>For example, let&#8217;s assume you have an edit action method with the following view:</p>
<p><a href="http://devermind.com/wp-content/uploads/2009/04/form1.png"><img class="alignnone size-full wp-image-103" title="EditForm" src="http://devermind.com/wp-content/uploads/2009/04/form1.png" alt="EditForm" width="274" height="334" /></a></p>
<p>So your edit form displays data coming from a Linq entity representing a customer (FirstName, LastName, Email, Country, AccountType), but you also need SelectLists to populate the Country and AccountType DropDownLists.</p>
<p><strong>The simple, but untyped approach: the ViewData dictionary</strong></p>
<p>The simplest approach is to use the Customer Linq entity as ViewModel and to put the two SelectLists in the ViewData dictionary. This works fine, but I dislike the fact that there is no type-safety for the two SelectLists. Instead you have to use a type cast from object to SelectList:</p>
<pre class="brush: xml;">
&lt;label for=&quot;CountryCode&quot;&gt;Country&lt;/label&gt;&lt;br /&gt;
&lt;%= Html.DropDownList(&quot;CountryCode&quot;,ViewData[&quot;Countries&quot;] as SelectList)%&gt;

&lt;%= Html.ValidationMessage(&quot;CountryCode&quot;, &quot;*&quot;)%&gt;
</pre>
<p><strong>The fully typed approach: Custom ViewModels</strong></p>
<p>So you want fully typed model data, but the linq entity does not hold all the data you need to render the view. Since you don&#8217;t want to clutter my Customer Linq to SQL entity with two properties that return the two SelectLists, the only alternative to get a fully typed view is to use a custom ViewModel:</p>
<pre class="brush: csharp;">public class CustomersFormViewModel
{
public SelectList AccountTypes { get; set; }
public SelectList Countries { get; set; }
public Customer Customer { get; set; }
}</pre>
<p>Note that the ViewModel is not a mere Linq entity anymore. Instead, it contains the one Customer Linq entity instance, plus the two SelectLists for my DropDownLists.</p>
<p>Now you can implement your view without using any typecasts:</p>
<pre class="brush: html;">

&lt;p&gt;

&lt;label for=&quot;Customer.FirstName&quot;&gt;

FirstName:&lt;/label&gt;&lt;br /&gt;

&lt;%= Html.TextBox(&quot;Customer.FirstName&quot;)%&gt;

&lt;%= Html.ValidationMessage(&quot;Customer.FirstName&quot;, &quot;*&quot;)%&gt;

&lt;/p&gt;

&lt;p&gt;

&lt;label for=&quot;Customer.LastName&quot;&gt;

LastName:&lt;/label&gt;&lt;br /&gt;

&lt;%= Html.TextBox(&quot;Customer.LastName&quot;)%&gt;

&lt;%= Html.ValidationMessage(&quot;Customer.LastName&quot;, &quot;*&quot;)%&gt;

&lt;/p&gt;

&lt;p&gt;

&lt;label for=&quot;Customer.Email&quot;&gt;

Email:&lt;/label&gt;&lt;br /&gt;

&lt;%= Html.TextBox(&quot;Customer.Email&quot;)%&gt;

&lt;%= Html.ValidationMessage(&quot;Customer.Email&quot;, &quot;*&quot;)%&gt;

&lt;/p&gt;

&lt;p&gt;

&lt;label for=&quot;Customer.CountryCode&quot;&gt;

Country&lt;/label&gt;&lt;br /&gt;

&lt;%= Html.DropDownList(&quot;Customer.CountryCode&quot;,Model.Countries)%&gt;

&lt;%= Html.ValidationMessage(&quot;Customer.CountryCode&quot;, &quot;*&quot;)%&gt;

&lt;/p&gt;

&lt;p&gt;

&lt;label for=&quot;Customer.AccountType&quot;&gt;

AccountType:&lt;/label&gt;&lt;br /&gt;

&lt;%= Html.DropDownList(&quot;Customer.AccountType&quot;,Model.AccountTypes)%&gt;

&lt;%= Html.ValidationMessage(&quot;Customer.AccountType&quot;, &quot;*&quot;)%&gt;

&lt;/p&gt;</pre>
<p>I was wondering if the MVC framework can automatically map the form values and reconstruct a CustomersFormViewModel. In other words, can I simply implement a strongly typed POST edit action method and receive a fully populated ViewModel as a parameter like this:</p>
<pre class="brush: csharp;">
//POST: /CustomersController/Edit

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(CustomersFormViewModel model)
{
//validate data, save customer, handle validation errors...
}</pre>
<p>The answer is yes! The MVC framework even maps object hierarchies of any depth as ViewModels.</p>
<p><strong>How not to do it</strong></p>
<p>This is all really easy, but still there were few things that threw me off at the beginning.</p>
<p>The ViewModelBinder only maps properties, not public fields. A ViewModel like this cannot be mapped:</p>
<pre class="brush: csharp;">public class CustomersFormViewModel
{
public electList AccountTypes;
public SelectList Countries;
public Customer Customer;
}</pre>
<p>The names of your POSTed form values must match the object hiearchy you want them to be mapped to. Something like this will not work:</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">
<pre class="brush: xhtml;">&lt;p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;

&lt;label for=&quot;FirstName&quot;&gt;

FirstName:&lt;/label&gt;&lt;br /&gt;

&lt;%= Html.TextBox(&quot;FirstName&quot;,Model,Customer,FirstName)%&gt;

&lt;%= Html.ValidationMessage(&quot;FirstName&quot;, &quot;*&quot;)%&gt;

&lt;/p&gt;</pre>
<p>In hindsight both restrictions are perfectly logic and reasonable, but it&#8217;s easy enough to do it wrong nevertheless and also quite difficult to figure out what went wrong if binding does not work. Hopefully this article will help you avoid any problems with using Custom ViewModels with POST action methods.</p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fdevermind.com%2flinq%2faspnet-mvc-using-custom-viewmodels-with-post-action-methods"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fdevermind.com%2flinq%2faspnet-mvc-using-custom-viewmodels-with-post-action-methods" border="0" alt="kick it on DotNetKicks.com" /></a></p>
<div class="shoutIt"><a rev="vote-for" href="http://dotnetshoutout.com/Submit?url=http://devermind.com/linq/aspnet-mvc-using-custom-viewmodels-with-post-action-methods"><br />
<img style="border:0px" src="http://dotnetshoutout.com/image.axd?url=http://devermind.com/linq/aspnet-mvc-using-custom-viewmodels-with-post-action-methods" alt="Shout it" /><br />
</a></div>
]]></content:encoded>
			<wfw:commentRss>http://devermind.com/linq/aspnet-mvc-using-custom-viewmodels-with-post-action-methods/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>ASP.NET MVC Beginner&#8217;s Guide</title>
		<link>http://devermind.com/aspnet-mvc/aspnet-mvc-beginners-guide/</link>
		<comments>http://devermind.com/aspnet-mvc/aspnet-mvc-beginners-guide/#comments</comments>
		<pubDate>Tue, 17 Mar 2009 12:19:23 +0000</pubDate>
		<dc:creator>Adrian Grigore</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[guide]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://devermind.com/?p=98</guid>
		<description><![CDATA[I&#8217;ve decided take Microsoft&#8217;s brand new ASP.NET MVC framework for a small test drive by building a small proof of concept application with it. The frame work sure looks promising, even despite it&#8217;s early stage of development and relatively small features as compared to ASP.NET web forms. 
It seems there are no good books available [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve decided take Microsoft&#8217;s brand new ASP.NET MVC framework for a small test drive by building a small proof of concept application with it. The frame work sure looks promising, even despite it&#8217;s early stage of development and relatively small features as compared to ASP.NET web forms. </p>
<p>It seems there are no good books available on the subject yet. The few that are already in stock at Amazon are based on the community preview, and it seems that there have been quite a few changes in the the framework since then. </p>
<p>Fortunately, ScottGu hat just released a free sample chapter of his upcoming &#8220;Professional ASP.NET&#8221; (Wrox) book. It contains a 200-page walkthrough guide on how to build a simple ASP.NET MVC application. </p>
<p>The chapter is available for download <a href="http://weblogs.asp.net/scottgu/archive/2009/03/10/free-asp-net-mvc-ebook-tutorial.aspx">here</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://devermind.com/aspnet-mvc/aspnet-mvc-beginners-guide/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
