<?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>em64t</title>
	<atom:link href="http://www.em64t.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.em64t.net</link>
	<description>random techno tidbits</description>
	<lastBuildDate>Sun, 22 May 2011 13:52:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>HDD vs SSD. Visual Studio benchmark.</title>
		<link>http://www.em64t.net/2011/05/hdd-vs-ssd-developer-tests/</link>
		<comments>http://www.em64t.net/2011/05/hdd-vs-ssd-developer-tests/#comments</comments>
		<pubDate>Wed, 04 May 2011 22:30:34 +0000</pubDate>
		<dc:creator>Evgeny</dc:creator>
				<category><![CDATA[Hardware]]></category>
		<category><![CDATA[HDD]]></category>
		<category><![CDATA[SSD]]></category>

		<guid isPermaLink="false">http://www.em64t.net/?p=72</guid>
		<description><![CDATA[I&#8217;ve been using two WD Raptor 10k RPM HDDs in stripe for 3 years or so. Recently, I finally ordered my first SSD (RealSSD C300 by Crucial). And I was really shocked by the overall performance gain it provided to the system. Not the advertized boot time, but rather typical developer daily workflows, like Visual]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been using two WD Raptor 10k RPM HDDs in stripe for 3 years or so.<br />
Recently, I finally ordered my first SSD (RealSSD C300 by Crucial). And I was really shocked by the overall performance gain it provided to the system. Not the advertized boot time, but rather typical  developer daily workflows, like Visual Studio tasks, MS SQL backup/restore/select/update/etc. and so on. Technically, the most important thing about SSDs in comparison to HDDs is that their performance doesn&#8217;t degrade in about two orders when it comes to random reads. So running several hard IO consuming tasks at once isn&#8217;t a problem at all. </p>
<p>I&#8217;ve made some tests before and after the switch and one video comparing Visual Studio load time with pretty heavy solution in it + ReSharper enabled:</p>
<p><iframe width="560" height="349" src="http://www.youtube.com/embed/_umDsjyvNvY" frameborder="0" allowfullscreen></iframe></p>
<p>As you see, VS load gets about 300% improvement. This is nice.</p>
<p>The CrystalDiskMark tests (notice the tremendous random IO difference):<br />
<span id="more-72"></span></p>
<p>WD Raptor 74GB 10000 RPM HDD (single one):<br />
<img src="http://habrastorage.org/storage/ec48ec40/bd3077ea/18bbf881/ca33ce77.png" style="box-shadow: 0 0 6px rgba(0, 0, 0, 0.6)"/></p>
<p>Crucial RealSSD C300 128G (SATA 3Gb/s mode):<br />
<img src="http://habrastorage.org/storage/63082fd3/63b0b151/6bffb1c6/8758099b.png" style="box-shadow: 0 0 6px rgba(0, 0, 0, 0.6)"/></p>
<p>Numbers speak for themselves.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.em64t.net/2011/05/hdd-vs-ssd-developer-tests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Organizing view models in ASP.NET MVC (Part 3 of 3)</title>
		<link>http://www.em64t.net/2011/03/organizing-view-models-in-asp-net-mvc-part-3-of-3/</link>
		<comments>http://www.em64t.net/2011/03/organizing-view-models-in-asp-net-mvc-part-3-of-3/#comments</comments>
		<pubDate>Sat, 12 Mar 2011 22:06:55 +0000</pubDate>
		<dc:creator>Evgeny</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[asp.net mvc]]></category>

		<guid isPermaLink="false">http://www.em64t.net/?p=31</guid>
		<description><![CDATA[This is the second part of Organizing view models in ASP.NET MVC (see Part 1, Part 2). In this part I’ll give a sample of designing view models for handling forms in MVC. Suppose that we need to create a controller and stuff around it to handle form like the following. For the sake of]]></description>
			<content:encoded><![CDATA[<p><strong>This is the second part of Organizing view models in ASP.NET MVC (see <a href="http://www.em64t.net/2011/01/organizing-view-models-in-asp-net-mvc-part-1-of-3/">Part 1</a>, <a href="http://www.em64t.net/2011/02/organizing-view-models-in-asp-net-mvc-part-2-of-3/">Part 2</a>).</strong></p>
<p>In this part I’ll give a sample of designing view models for handling forms in MVC. Suppose that we need to create a controller and stuff around it to handle form like the following.</p>
<p>For the sake of demo simplicity I keep the form simple:<br />
<img src="http://habrastorage.org/storage/f16dcedf/83adda84/c4391e95/affd1a1d.png" alt="" /><br />
<span id="more-31"></span></p>
<p>For this, we add View that specifies <code>AccountRegisterViewModel </code>as its model:</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #006600; font-weight: bold;">@</span>model AccountRegisterViewModel
&nbsp;
<span style="color: #006600; font-weight: bold;">&lt;</span>h2<span style="color: #006600; font-weight: bold;">&gt;</span>
    Register<span style="color: #006600; font-weight: bold;">&lt;/</span>h2<span style="color: #006600; font-weight: bold;">&gt;</span>
&nbsp;
<span style="color: #006600; font-weight: bold;">@</span>using <span style="color: #006600; font-weight:bold;">&#40;</span>Html.<span style="color: #9900cc;">BeginForm</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
<span style="color: #006600; font-weight:bold;">&#123;</span>
    <span style="color: #006600; font-weight: bold;">@</span>Html.<span style="color: #9900cc;">ValidationSummary</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
    <span style="color: #006600; font-weight: bold;">&lt;</span>label<span style="color: #006600; font-weight: bold;">&gt;</span>
        Name
        <span style="color: #006600; font-weight: bold;">@</span>Html.<span style="color: #9900cc;">TextBoxFor</span><span style="color: #006600; font-weight:bold;">&#40;</span>m <span style="color: #006600; font-weight: bold;">=&gt;</span> m.<span style="color: #330066;">Form</span>.<span style="color: #9900cc;">Name</span><span style="color: #006600; font-weight:bold;">&#41;</span>
    <span style="color: #006600; font-weight: bold;">&lt;/</span>label<span style="color: #006600; font-weight: bold;">&gt;</span>
    <span style="color: #006600; font-weight: bold;">&lt;</span>label<span style="color: #006600; font-weight: bold;">&gt;</span>
        State
        <span style="color: #006600; font-weight: bold;">@</span>Html.<span style="color: #9900cc;">DropDownListFor</span><span style="color: #006600; font-weight:bold;">&#40;</span>m <span style="color: #006600; font-weight: bold;">=&gt;</span> m.<span style="color: #330066;">Form</span>.<span style="color: #9900cc;">State</span>, Model.<span style="color: #9900cc;">StateSelectList</span><span style="color: #006600; font-weight:bold;">&#41;</span>
    <span style="color: #006600; font-weight: bold;">&lt;/</span>label<span style="color: #006600; font-weight: bold;">&gt;</span>
    <span style="color: #006600; font-weight: bold;">&lt;</span>input type<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;submit&quot;</span> value<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;Register me&quot;</span> <span style="color: #006600; font-weight: bold;">/&gt;</span>
<span style="color: #006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>Then, we add a new view model class to <em>ViewModels/Account</em> directory. It contains all the data required to render the form, including drop down list data, and properties of the form to submit (in this sample we have a single form under Form property, but if you have multiple forms, then you can place each one under separate <code>FormXXX</code> property):</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> AccountRegisterViewModel <span style="color: #008000;">:</span> SharedLayoutViewModel
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> AccountRegisterForm Form <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> IEnumerable<span style="color: #008000;">&lt;</span>SelectListItem<span style="color: #008000;">&gt;</span> StateSelectList <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>We define a class that contains all the fields of our form. Later, we’ll be able to use strongly-typed helpers to render inputs. The class contains <code>DataAnnotations </code>attributes for form validation, and for other  cases it can implement the <code>IValidatableObject </code>interface. So we have all the object level validation for the form in a single place. Nice and clean:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> AccountRegisterForm
<span style="color: #008000;">&#123;</span>
    <span style="color: #008000;">&#91;</span>Required<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Name <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008000;">&#91;</span>Required<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> State <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The controller itself looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>HttpGet<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> ActionResult Register<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    var model <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> AccountRegisterViewModel<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    SetupRegisterViewModel<span style="color: #008000;">&#40;</span>model<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> View<span style="color: #008000;">&#40;</span>model<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">&#91;</span>HttpPost<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> ActionResult Register<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#91;</span>Bind<span style="color: #008000;">&#40;</span>Include <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Form&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>AccountRegisterViewModel model<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>ModelState<span style="color: #008000;">.</span><span style="color: #0000FF;">IsValid</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// save model.Form </span>
&nbsp;
        TempData<span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;Message&quot;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Thank you for registering&quot;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> RedirectToAction<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Index&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    SetupRegisterViewModel<span style="color: #008000;">&#40;</span>model<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> View<span style="color: #008000;">&#40;</span>model<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> SetupRegisterViewModel<span style="color: #008000;">&#40;</span>AccountRegisterViewModel model<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    model<span style="color: #008000;">.</span><span style="color: #0000FF;">StateSelectList</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SelectList<span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Empty</span>, <span style="color: #666666;">&quot;NY&quot;</span>, <span style="color: #666666;">&quot;VA&quot;</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>That’s it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.em64t.net/2011/03/organizing-view-models-in-asp-net-mvc-part-3-of-3/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Organizing view models in ASP.NET MVC (Part 2 of 3)</title>
		<link>http://www.em64t.net/2011/02/organizing-view-models-in-asp-net-mvc-part-2-of-3/</link>
		<comments>http://www.em64t.net/2011/02/organizing-view-models-in-asp-net-mvc-part-2-of-3/#comments</comments>
		<pubDate>Tue, 01 Feb 2011 21:46:31 +0000</pubDate>
		<dc:creator>Evgeny</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[asp.net mvc]]></category>

		<guid isPermaLink="false">http://www.em64t.net/?p=19</guid>
		<description><![CDATA[This is the second part of Organizing view models in ASP.NET MVC (see Part 1). So, designing classes for your view models inevitably brings up some new concerns, like how should the Layout view models be implemented (master pages in terms of WebForms view engine). From my experience, you should create an abstract class like]]></description>
			<content:encoded><![CDATA[<p><strong>This is the second part of Organizing view models in ASP.NET MVC (see <a href="http://www.em64t.net/2011/01/organizing-view-models-in-asp-net-mvc-part-1-of-3/">Part 1</a>).</strong></p>
<p>So, designing classes for your view models inevitably brings up some new concerns, like how should the Layout view models be implemented (<em>master pages</em> in terms of WebForms view engine).</p>
<p>From my experience, you should create an abstract class like <code>SharedLayoutViewModel </code>with all of the data properties required by your <code>_Lauout.cshtm</code>l. Then, make all of the view models of views that use this layout inherit this class. You can use MVC Result filters to fill this model with data in a single place of your app, or, like I do, override the <code>OnResultExecuting </code>method in the common base class for your controllers, and fill it there.</p>
<p>To make your view models easy to find, organize them in a similar fashion as you do for views. I prefer putting all the view models in a single namespace (though in different project directories) and having full name as its class name like <code>ProductsCreateViewModel </code>(if you’re using ReSharper, set <code>NamespaceProvider = False</code>). This way I avoid need of explicit namespace reference in views and controllers. So my final layout looks like this:</p>
<p><img src="http://habrastorage.org/storage/401b85fa/bd71b822/67f7e2b2/677695b4.png" alt="" /></p>
<p>In the next part I’ll give a sample of a simple form implemented using the described approach.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.em64t.net/2011/02/organizing-view-models-in-asp-net-mvc-part-2-of-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.NET MVC bug binding collection of DropDownList?</title>
		<link>http://www.em64t.net/2011/01/asp-net-mvc-bug-binding-collection-of-dropdownlist/</link>
		<comments>http://www.em64t.net/2011/01/asp-net-mvc-bug-binding-collection-of-dropdownlist/#comments</comments>
		<pubDate>Wed, 26 Jan 2011 06:38:21 +0000</pubDate>
		<dc:creator>Evgeny</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[asp.net mvc]]></category>
		<category><![CDATA[bug]]></category>

		<guid isPermaLink="false">http://www.em64t.net/?p=49</guid>
		<description><![CDATA[Recently, while experimenting with strongly-typed form helpers of MVC, I found a bug. I posted a question at SO, but with no luck so far. Generally, looks like DropDownFor is broken when trying to bind collection of form drop downs. I have a workaround in form of using non-typed helper, but anyway hope to get]]></description>
			<content:encoded><![CDATA[<p>Recently, while experimenting with strongly-typed form helpers of MVC, I found a bug.<br />
I posted a question at SO, but with no luck so far.<br />
Generally, looks like <code>DropDownFor</code> is broken when trying to bind collection of form drop downs.<br />
I have a workaround in form of using non-typed helper, but anyway hope to get things sorted out with strongly-typed one.</p>
<p><span id="more-49"></span></p>
<p>So. The bug.<br />
I have a view with 1 dropdown generated from Model property and 3 additional dropdowns that are generated from array property.</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #006600; font-weight: bold;">@</span>Html.<span style="color: #9900cc;">DropDownListFor</span><span style="color: #006600; font-weight:bold;">&#40;</span>m <span style="color: #006600; font-weight: bold;">=&gt;</span> m.<span style="color: #9900cc;">AgentType</span>, Model.<span style="color: #9900cc;">AgentTypeListItems</span><span style="color: #006600; font-weight:bold;">&#41;</span>
<span style="color: #006600; font-weight: bold;">@</span><span style="color: #990099; font-weight: bold;">for</span> <span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #990099; font-weight: bold;">int</span> i <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #800000;">0</span><span style="color: #006600; font-weight: bold;">;</span> i <span style="color: #006600; font-weight: bold;">&lt;</span> Model.<span style="color: #9900cc;">AgentTypes</span>.<span style="color: #9900cc;">Length</span><span style="color: #006600; font-weight: bold;">;</span> i++<span style="color: #006600; font-weight:bold;">&#41;</span>
<span style="color: #006600; font-weight:bold;">&#123;</span>
    <span style="color: #006600; font-weight: bold;">@</span>Html.<span style="color: #9900cc;">DropDownListFor</span><span style="color: #006600; font-weight:bold;">&#40;</span>m <span style="color: #006600; font-weight: bold;">=&gt;</span> m.<span style="color: #9900cc;">AgentTypes</span><span style="color: #006600; font-weight:bold;">&#91;</span>i<span style="color: #006600; font-weight:bold;">&#93;</span>, Model.<span style="color: #9900cc;">AgentTypeListItems</span><span style="color: #006600; font-weight:bold;">&#41;</span>
<span style="color: #006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>The controller method initializes AgentTypeListItems collection + sets default values for AgentType dropdown and 3 dropdowns for the collection:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var model <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> OptionsViewModel<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// for DropDownListFor</span>
model<span style="color: #008000;">.</span><span style="color: #0000FF;">AgentTypeListItems</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #008000;">new</span> SelectListItem <span style="color: #008000;">&#123;</span> Text <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;1&quot;</span>, Value <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;1&quot;</span> <span style="color: #008000;">&#125;</span>, 
    <span style="color: #008000;">new</span> SelectListItem <span style="color: #008000;">&#123;</span> Text <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;2&quot;</span>, Value <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;2&quot;</span> <span style="color: #008000;">&#125;</span>,
    <span style="color: #008000;">new</span> SelectListItem <span style="color: #008000;">&#123;</span> Text <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;3&quot;</span>, Value <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;3&quot;</span> <span style="color: #008000;">&#125;</span>,
<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// 1 dropdown in the model</span>
model<span style="color: #008000;">.</span><span style="color: #0000FF;">AgentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;2&quot;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// 3 dropdowns in array</span>
model<span style="color: #008000;">.</span><span style="color: #0000FF;">AgentTypes</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">&quot;3&quot;</span>, <span style="color: #666666;">&quot;2&quot;</span>, <span style="color: #666666;">&quot;1&quot;</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">return</span> View<span style="color: #008000;">&#40;</span>model<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>The model class:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> OptionsViewModel
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> SelectListItem<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> AgentTypeListItems <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> AgentType <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> AgentTypes <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>When I open it in browser I get &#8220;2&#8243; everywhere though AgentTypes array was initialized with different values(!):</p>
<p><img src="http://i.imgur.com/2HSvi.png" alt="" /></p>
<p>When I replace DropDownListFor with TextBoxFor:</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #006600; font-weight: bold;">@</span>Html.<span style="color: #9900cc;">TextBoxFor</span><span style="color: #006600; font-weight:bold;">&#40;</span>m <span style="color: #006600; font-weight: bold;">=&gt;</span> m.<span style="color: #9900cc;">AgentTypes</span><span style="color: #006600; font-weight:bold;">&#91;</span>i<span style="color: #006600; font-weight:bold;">&#93;</span><span style="color: #006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>I get the correct values in the inputs (!):</p>
<p>this is how it should be</p>
<p><img src="http://i.imgur.com/hJbDQ.png" alt="" /></p>
<p>It means the TextBoxFor works as expected, but DropDownListFor doesn&#8217;t.<br />
Will try to post the issue into MVC project bugtracker.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.em64t.net/2011/01/asp-net-mvc-bug-binding-collection-of-dropdownlist/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Organizing view models in ASP.NET MVC (Part 1 of 3)</title>
		<link>http://www.em64t.net/2011/01/organizing-view-models-in-asp-net-mvc-part-1-of-3/</link>
		<comments>http://www.em64t.net/2011/01/organizing-view-models-in-asp-net-mvc-part-1-of-3/#comments</comments>
		<pubDate>Wed, 12 Jan 2011 20:52:54 +0000</pubDate>
		<dc:creator>Evgeny</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[asp.net mvc]]></category>
		<category><![CDATA[design]]></category>

		<guid isPermaLink="false">http://www.em64t.net/?p=6</guid>
		<description><![CDATA[The internet is filled up with video tutorials like “Creating wiki using ASP.NET MVC in 15 minutes”. The problem of these tutorials is that they do not cover real life scenarios. They tend to use ViewData and ViewBag to transfer data into View. They use non-typed Html.TextBox() helper methods for generating forms. They use controller]]></description>
			<content:encoded><![CDATA[<p>The internet is filled up with video tutorials like “Creating wiki using ASP.NET MVC in 15 minutes”. The problem of these tutorials is that they do not cover real life scenarios. They tend to use <code>ViewData</code> and <code>ViewBag</code> to transfer data into View. They use non-typed <code>Html.TextBox()</code> helper methods for generating forms. They use controller method parameters to transfer form data back to the controller one by one. Or, more worse, they directly put ORM entity into controller method parameters, which can lead to huge problems when practiced by a non-experienced developer.</p>
<p>So, such “rapid” approaches can be attractive for the person, whose job is to create such video tutorials (probably trying to compete with the famous RoR one).</p>
<p>But in a real life applications, you’ll probably want to use strongly-typed models,  strongly-typed model-aware <code>Html.TextBoxFor(m =&gt; ..)</code> helpers for generating forms, and get strongly-typed POST data into controller method. In other words, you need acceptable maintainability for your projects in MVC.</p>
<p><strong>So here are the rules to remember, if you’re going to develop with real MVC.</strong></p>
<ol>
<li>Views to ViewModels is a one-to-one relationship. Define separate ViewModel class for every single View.</li>
<li>View dictates ViewModel structure, not the controller.</li>
<li>ViewModles are dummy DTOs, with no logic in them.</li>
<li>Forget about calling <code>Request.IsAuthenticated</code> and similar in your views. ViewModel should contain ALL the data required to render the view.</li>
</ol>
<p>In my next post I’ll describe how you can use  strongly-typed models in generic view layouts (master pages in MVC2). See <a href="http://www.em64t.net/2011/02/organizing-view-models-in-asp-net-mvc-part-2-of-3/">Part 2</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.em64t.net/2011/01/organizing-view-models-in-asp-net-mvc-part-1-of-3/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Razor: Html.RenderPartial vs Html.Partial, Html.RenderAction vs Html.Action &#8211; what one should use?</title>
		<link>http://www.em64t.net/2010/12/razor-html-renderpartial-vs-html-partial-html-renderaction-vs-html-action-what-one-should-use/</link>
		<comments>http://www.em64t.net/2010/12/razor-html-renderpartial-vs-html-partial-html-renderaction-vs-html-action-what-one-should-use/#comments</comments>
		<pubDate>Wed, 29 Dec 2010 22:54:06 +0000</pubDate>
		<dc:creator>Evgeny</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[asp.net mvc]]></category>
		<category><![CDATA[razor]]></category>

		<guid isPermaLink="false">http://www.em64t.net/?p=93</guid>
		<description><![CDATA[This question can be confusing for people starting using ASP.NET MVC3. Today, we have two ways to render partial and action stuff: @Html.Partial&#40;&#34;Details&#34;&#41; @Html.Action&#40;&#34;Index&#34;, &#34;Home&#34;&#41; and @&#123; Html.RenderPartial&#40;&#34;Details&#34;&#41;; &#125; @&#123; Html.RenderAction&#40;&#34;Index&#34;, &#34;Home&#34;&#41;; &#125; So what would you go with? First approach looks cleaner, but I would use RenderPartial and RenderAction in most cases, just because]]></description>
			<content:encoded><![CDATA[<p>This question can be confusing for people starting using ASP.NET MVC3. </p>
<p>Today, we have two ways to render partial and action stuff:</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #006600; font-weight: bold;">@</span>Html.<span style="color: #9900cc;">Partial</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;Details&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
<span style="color: #006600; font-weight: bold;">@</span>Html.<span style="color: #9900cc;">Action</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;Index&quot;</span>, <span style="color: #cc0000;">&quot;Home&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>and</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #006600; font-weight: bold;">@</span><span style="color: #006600; font-weight:bold;">&#123;</span> Html.<span style="color: #9900cc;">RenderPartial</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;Details&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span> <span style="color: #006600; font-weight:bold;">&#125;</span>
<span style="color: #006600; font-weight: bold;">@</span><span style="color: #006600; font-weight:bold;">&#123;</span> Html.<span style="color: #9900cc;">RenderAction</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;Index&quot;</span>, <span style="color: #cc0000;">&quot;Home&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span> <span style="color: #006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>So what would you go with?<br />
<span id="more-93"></span></p>
<p>First approach looks cleaner, but I would use <code>RenderPartial</code> and <code>RenderAction</code> in most cases, just because these methods output their content into the same <code>TextWriter</code> object as used in the current template. In contrast to this, <code>@Html.Partial</code> and <code>@Html.Action</code> methods create their own <code>TextWriter</code> instances each time and buffer all their content into memory. In the final stage they just emit all writer content it into a plain <code>MvcString</code> object, so that it can be rendered as an unescaped HTML by the <code>@</code> directive.</p>
<p>On the other side, I do not entirely through away <code>@Html.Partial</code> and <code>@Html.Action</code>. These methods are good for some cases when you need to put sub-template content into a variable or do something with it (which was a pain in older MVC versions). And they may be life-saving in certain scenarios like caching.</p>
<p>Oh, there is also a <code>@RenderPage</code> method of <code>WebPageBase</code> that doesn&#8217;t use MVC template lookup and receives exact template path as its parameter. But anyway I would use HtmlHelper&#8217;s Partial instead, as it is a more traditional view-engine independent approach for rendering child templates.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.em64t.net/2010/12/razor-html-renderpartial-vs-html-partial-html-renderaction-vs-html-action-what-one-should-use/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>MVC Recipe: Using strongly-typed html helpers in @helper</title>
		<link>http://www.em64t.net/2010/07/mvc-using-strongly-typed-html-helpers-in-helper/</link>
		<comments>http://www.em64t.net/2010/07/mvc-using-strongly-typed-html-helpers-in-helper/#comments</comments>
		<pubDate>Tue, 20 Jul 2010 21:36:24 +0000</pubDate>
		<dc:creator>Evgeny</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[asp.net mvc]]></category>

		<guid isPermaLink="false">http://www.em64t.net/?p=60</guid>
		<description><![CDATA[Razor is a brand new view engine for MVC. I&#8217;ll describe a method to use strongly-typed helpers like TextBoxFor(m=>m.FirstName) in its reusable @helper blocks If you never heard about it, then read on ScottGu blog. Suppose you have a checkout model that has two addresses &#8211; billing and shipping: public class CheckoutInfoForm &#123; public CheckoutAddressForm]]></description>
			<content:encoded><![CDATA[<p>Razor is a brand new view engine for MVC. I&#8217;ll describe a method to use strongly-typed helpers like <code>TextBoxFor(m=>m.FirstName)</code> in its reusable @helper blocks<br />
If you never heard about it, then read on <a href="http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx">ScottGu blog.</a></p>
<p>Suppose you have a checkout model that has two addresses &#8211; billing and shipping:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> CheckoutInfoForm
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> CheckoutAddressForm BillingAddress <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> CheckoutAddressForm ShippingAddress <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>While each address has model like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> CheckoutAddressForm
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> FirstName <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> LastName <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Address1 <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// and so on</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>How would you generate address fields for both and reuse the markup?<br />
<span id="more-60"></span><br />
The correct answer is: use the <code>@helper</code> block. I know another good option (or even may be a better in this particular case) is using <em>Edit </em>template for such kind of things, but let&#8217;s assume that you selected @helper way for good some reason.</p>
<p>So here is the code of Razor view:</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #006600; font-weight: bold;">&lt;</span>form method<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;post&quot;</span><span style="color: #006600; font-weight: bold;">&gt;</span>
<span style="color: #006600; font-weight: bold;">&lt;</span>div<span style="color: #006600; font-weight: bold;">&gt;</span>
    <span style="color: #006600; font-weight: bold;">&lt;</span>h1<span style="color: #006600; font-weight: bold;">&gt;</span>
        Billing Address<span style="color: #006600; font-weight: bold;">&lt;/</span>h1<span style="color: #006600; font-weight: bold;">&gt;</span>
    <span style="color: #006600; font-weight: bold;">@</span>RenderAddressFor<span style="color: #006600; font-weight:bold;">&#40;</span>t <span style="color: #006600; font-weight: bold;">=&gt;</span> t.<span style="color: #330066;">Form</span>.<span style="color: #9900cc;">BillingAddress</span><span style="color: #006600; font-weight:bold;">&#41;</span>
<span style="color: #006600; font-weight: bold;">&lt;/</span>div<span style="color: #006600; font-weight: bold;">&gt;</span>
<span style="color: #006600; font-weight: bold;">&lt;</span>div<span style="color: #006600; font-weight: bold;">&gt;</span>
    <span style="color: #006600; font-weight: bold;">&lt;</span>h1<span style="color: #006600; font-weight: bold;">&gt;</span>
        Shipping Address<span style="color: #006600; font-weight: bold;">&lt;/</span>h1<span style="color: #006600; font-weight: bold;">&gt;</span>
    <span style="color: #006600; font-weight: bold;">@</span>RenderAddressFor<span style="color: #006600; font-weight:bold;">&#40;</span>t <span style="color: #006600; font-weight: bold;">=&gt;</span> t.<span style="color: #330066;">Form</span>.<span style="color: #9900cc;">ShippingAddress</span><span style="color: #006600; font-weight:bold;">&#41;</span>
<span style="color: #006600; font-weight: bold;">&lt;/</span>div<span style="color: #006600; font-weight: bold;">&gt;</span>
<span style="color: #006600; font-weight: bold;">&lt;</span>input type<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;submit&quot;</span> <span style="color: #006600; font-weight: bold;">/&gt;</span>
<span style="color: #006600; font-weight: bold;">&lt;/</span>form<span style="color: #006600; font-weight: bold;">&gt;</span>
<span style="color: #006600; font-weight: bold;">@</span>helper RenderAddressFor<span style="color: #006600; font-weight:bold;">&#40;</span>Expression<span style="color: #006600; font-weight: bold;">&lt;</span>Func<span style="color: #006600; font-weight: bold;">&lt;</span>CheckoutInfoViewModel, CheckoutAddressForm<span style="color: #006600; font-weight: bold;">&gt;&gt;</span> expr<span style="color: #006600; font-weight:bold;">&#41;</span>
    <span style="color: #006600; font-weight:bold;">&#123;</span>
        <span style="color: #0000ff; font-weight: bold;">var</span> convert <span style="color: #006600; font-weight: bold;">=</span> SubformUtil.<span style="color: #9900cc;">GetConverter</span><span style="color: #006600; font-weight: bold;">&lt;</span>CheckoutInfoViewModel, CheckoutAddressForm<span style="color: #006600; font-weight: bold;">&gt;</span><span style="color: #006600; font-weight:bold;">&#40;</span>expr<span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
&nbsp;
    <span style="color: #006600; font-weight: bold;">&lt;</span>label<span style="color: #006600; font-weight: bold;">&gt;</span>
        First Name
        <span style="color: #006600; font-weight: bold;">@</span>Html.<span style="color: #9900cc;">TextBoxFor</span><span style="color: #006600; font-weight:bold;">&#40;</span>convert<span style="color: #006600; font-weight:bold;">&#40;</span>t <span style="color: #006600; font-weight: bold;">=&gt;</span> t.<span style="color: #9900cc;">FirstName</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
    <span style="color: #006600; font-weight: bold;">&lt;/</span>label<span style="color: #006600; font-weight: bold;">&gt;</span>
    <span style="color: #006600; font-weight: bold;">&lt;</span>label<span style="color: #006600; font-weight: bold;">&gt;</span>
        Last Name
        <span style="color: #006600; font-weight: bold;">@</span>Html.<span style="color: #9900cc;">TextBoxFor</span><span style="color: #006600; font-weight:bold;">&#40;</span>convert<span style="color: #006600; font-weight:bold;">&#40;</span>t <span style="color: #006600; font-weight: bold;">=&gt;</span> t.<span style="color: #9900cc;">LastName</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
    <span style="color: #006600; font-weight: bold;">&lt;/</span>label<span style="color: #006600; font-weight: bold;">&gt;</span>
    <span style="color: #006600; font-weight: bold;">&lt;</span>label<span style="color: #006600; font-weight: bold;">&gt;</span>
        Address1
        <span style="color: #006600; font-weight: bold;">@</span>Html.<span style="color: #9900cc;">TextBoxFor</span><span style="color: #006600; font-weight:bold;">&#40;</span>convert<span style="color: #006600; font-weight:bold;">&#40;</span>t <span style="color: #006600; font-weight: bold;">=&gt;</span> t.<span style="color: #9900cc;">Address1</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
    <span style="color: #006600; font-weight: bold;">&lt;/</span>label<span style="color: #006600; font-weight: bold;">&gt;</span>
<span style="color: #006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>So far so good, but notice this little <code>SubformUtil.GetConverter</code> call. It is a simple shortcut creator function that I moved to a helper tool class:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Linq.Expressions</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">LinqKit</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> SubformUtil
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> Func<span style="color: #008000;">&lt;</span>Expression<span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span>TFormModel, <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;&gt;</span>, Expression<span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span>TViewModel, <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;&gt;&gt;</span>
        GetConverter<span style="color: #008000;">&lt;</span>TViewModel, TFormModel<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>Expression<span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span>TViewModel, TFormModel<span style="color: #008000;">&gt;&gt;</span> expr<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> m <span style="color: #008000;">=&gt;</span> Linq<span style="color: #008000;">.</span><span style="color: #0000FF;">Expr</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>TViewModel vm<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> m<span style="color: #008000;">.</span><span style="color: #0000FF;">Invoke</span><span style="color: #008000;">&#40;</span>expr<span style="color: #008000;">.</span><span style="color: #0000FF;">Invoke</span><span style="color: #008000;">&#40;</span>vm<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Expand</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Expand</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Also notice, that I&#8217;m using a wonderful <a href="http://www.albahari.com/nutshell/linqkit.aspx">LinqKit</a> utility to generate the expression. That&#8217;s all. Hope this will be helpful.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.em64t.net/2010/07/mvc-using-strongly-typed-html-helpers-in-helper/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>T-SQL: Using OUTPUT clause to insert original value.</title>
		<link>http://www.em64t.net/2009/12/t-sql-using-output-clause-to-insert-original-value/</link>
		<comments>http://www.em64t.net/2009/12/t-sql-using-output-clause-to-insert-original-value/#comments</comments>
		<pubDate>Tue, 22 Dec 2009 22:43:02 +0000</pubDate>
		<dc:creator>Evgeny</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[t-sql]]></category>

		<guid isPermaLink="false">http://www.em64t.net/?p=102</guid>
		<description><![CDATA[The OUTPUT clause is the obvious choice in cases when you need to output what you insert/update/delete something from the database. You can use DELETED and INSERTED prefixes to refer inserted and deleted rows. But what if you want to insert rows in to a table and OUTPUT a field from the source table that]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://msdn.microsoft.com/en-us/library/ms177564.aspx">OUTPUT clause</a> is the obvious choice in cases when you need to output what you insert/update/delete something from the database. </p>
<p>You can use  <em>DELETED </em>and <em>INSERTED </em>prefixes to refer inserted and deleted rows. But what if you want to insert rows in to a table and OUTPUT a field from the source table that you DO NOT insert into destination table (and thus the field is not available through the <em>INSERTED </em>prefix).</p>
<p>Assume the following scenario. You have a table variable where you want to put identifiers of the source and destination records (to use those later):</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">DECLARE</span> @InsertedAnimals <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #66cc66;">&#40;</span>AnimalID <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">,</span> @CatID <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>And you want to execute a statement like the below to insert cats, where Cat is a table of <code>CatID (identity), Name (varchar)</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> Cat <span style="color: #66cc66;">&#40;</span>Name<span style="color: #66cc66;">&#41;</span> OUTPUT Animal<span style="color: #66cc66;">.</span>AnimalID<span style="color: #66cc66;">,</span> INSERTED<span style="color: #66cc66;">.</span>CatID <span style="color: #993333; font-weight: bold;">INTO</span> @InsertedAnimals
<span style="color: #993333; font-weight: bold;">SELECT</span> Name <span style="color: #993333; font-weight: bold;">FROM</span> Animal <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #993333; font-weight: bold;">TYPE</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Cat'</span>;</pre></div></div>

<p>So that <code>@InsertedAnimals</code> would contain all the inserted <code>AnimalID </code>to <code>CatID </code>references. Obviously, this would not work, because you have no access to the Animal table in the <code>OUTPUT </code>clause. Only <code>INSERTED </code>prefix is available there.</p>
<p>BUT, there is a solution. <span id="more-102"></span></p>
<p>We can use the <a href="http://msdn.microsoft.com/en-us/library/bb510625.aspx">MERGE statement</a> instead:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">MERGE</span> Cat <span style="color: #993333; font-weight: bold;">AS</span> t 
<span style="color: #993333; font-weight: bold;">USING</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> Name <span style="color: #993333; font-weight: bold;">FROM</span> Items <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #993333; font-weight: bold;">TYPE</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Cat'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> s
<span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span> 
<span style="color: #993333; font-weight: bold;">WHEN</span> <span style="color: #993333; font-weight: bold;">NOT</span> MATCHED <span style="color: #993333; font-weight: bold;">BY</span> TARGET <span style="color: #993333; font-weight: bold;">THEN</span>
  <span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #66cc66;">&#40;</span>Name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span>Name<span style="color: #66cc66;">&#41;</span>
OUTPUT s<span style="color: #66cc66;">.</span>AnimalID<span style="color: #66cc66;">,</span> INSERTED<span style="color: #66cc66;">.</span>CatID <span style="color: #993333; font-weight: bold;">INTO</span> @InsertedAnimals;</pre></div></div>

<p>Easy, reliable and clean. A very nice alternative to using cursor, or locking tables and doing multiple requests and use ROW_NUMBER to determine <em>inserted&lt;&mdash;&gt;original</em> id references, etc.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.em64t.net/2009/12/t-sql-using-output-clause-to-insert-original-value/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

