<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Dave Arlin - Bloggin' - ASP.Net MVC</title>
    <link>http://blog.davearlin.com/</link>
    <description>learn forever</description>
    <language>en-us</language>
    <copyright>Dave Arlin</copyright>
    <lastBuildDate>Mon, 03 May 2010 14:54:25 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.2.8279.16125</generator>
    <managingEditor>dave@davearlin.com</managingEditor>
    <webMaster>dave@davearlin.com</webMaster>
    <item>
      <trackback:ping>http://blog.davearlin.com/Trackback.aspx?guid=915a5fb7-6059-43dd-ace5-635e12347ebb</trackback:ping>
      <pingback:server>http://blog.davearlin.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.davearlin.com/PermaLink,guid,915a5fb7-6059-43dd-ace5-635e12347ebb.aspx</pingback:target>
      <dc:creator>Dave Arlin</dc:creator>
      <wfw:comment>http://blog.davearlin.com/CommentView,guid,915a5fb7-6059-43dd-ace5-635e12347ebb.aspx</wfw:comment>
      <wfw:commentRss>http://blog.davearlin.com/SyndicationService.asmx/GetEntryCommentsRss?guid=915a5fb7-6059-43dd-ace5-635e12347ebb</wfw:commentRss>
      <title>Simple Tip for Accessing Relative Path Items in Javascript in your ASP.NET Website</title>
      <guid isPermaLink="false">http://blog.davearlin.com/PermaLink,guid,915a5fb7-6059-43dd-ace5-635e12347ebb.aspx</guid>
      <link>http://blog.davearlin.com/2010/05/03/SimpleTipForAccessingRelativePathItemsInJavascriptInYourASPNETWebsite.aspx</link>
      <pubDate>Mon, 03 May 2010 14:54:25 GMT</pubDate>
      <description>&lt;p&gt;
One common problem I’ve faced when developing applications is when I need to access
an Image file or some other artifact that is relative to my website.
&lt;/p&gt;
&lt;p&gt;
I’ve often gotten “lucky” where my virtual directory path locally matches my QA, Staging,
Production, etc. So I’m able to have javascript code like this:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:0062998b-391c-47d2-92a9-d6cd0190ff53" class="wlWriterEditableSmartContent"&gt;
&lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt;
&lt;div style="background: #fff; max-height: 300px; overflow: auto"&gt;
&lt;ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt;
&lt;li&gt;
$(&lt;span style="color:#a31515"&gt;'.ok-button'&lt;/span&gt;).attr(&lt;span style="color:#a31515"&gt;'src'&lt;/span&gt;, &lt;span style="color:#a31515"&gt;'/Content/Images/btn_ok.jpg'&lt;/span&gt;);&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
$(&lt;span style="color:#a31515"&gt;'.ok-button'&lt;/span&gt;).hover(&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;function&lt;/span&gt;() { $(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;).attr(&lt;span style="color:#a31515"&gt;'src'&lt;/span&gt;, &lt;span style="color:#a31515"&gt;'/Content/Images/btn_okOver.jpg'&lt;/span&gt;);
}&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
, &lt;span style="color:#0000ff"&gt;function&lt;/span&gt;() { $(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;).attr(&lt;span style="color:#a31515"&gt;'src'&lt;/span&gt;, &lt;span style="color:#a31515"&gt;'/Content/Images/btn_ok.jpg'&lt;/span&gt;);
}&lt;/li&gt;
&lt;li&gt;
);&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
Here, I’m assuming that my images will always be stored somehow like this:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://localhost/Content/Images"&gt;http://localhost/Content/Images&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://[test-dns]/Content/Images"&gt;http://[test-dns]/Content/Images&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://[prod-dns]/Content/Images"&gt;http://[prod-dns]/Content/Images&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
etc…
&lt;/p&gt;
&lt;p&gt;
That above javascript will fail if I am forced to be put into a Virtual Directory
such as this:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://[prod-dns]/[some-virtual-directory]/Content/Images"&gt;http://[prod-dns]/[some-virtual-directory]/Content/Images&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
The most effective way I’ve found to get around this is by creating a hidden field
on my main master page that contains the value derived by the web server of the root
URL to my website. This root URL might contain no virtual directory or 10 virtual
directories depending on the IIS Web Server set up.
&lt;/p&gt;
&lt;p&gt;
I’m using ASP.NET MVC, but this example can easily be ported into a Page_Load event
with standard ASP.NET web forms.
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:52f40903-a23d-4f2e-87fc-e84a8355e13c" class="wlWriterEditableSmartContent"&gt;
&lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt;
&lt;div style="background: #fff; max-height: 300px; overflow: auto"&gt;
&lt;ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px;"&gt;
&lt;li&gt;
&lt;span style="background:#ffee62"&gt;&lt;%&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; virtualPath = &lt;span style="color:#2b91af"&gt;HttpRuntime&lt;/span&gt;.AppDomainAppVirtualPath;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;       virtualPath = virtualPath.EndsWith(&lt;span style="color:#a31515"&gt;"/"&lt;/span&gt;) ? virtualPath : virtualPath + &lt;span style="color:#a31515"&gt;"/"&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffee62"&gt;%&gt;&lt;/span&gt;
&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;&lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;input&lt;/span&gt; &lt;span style="color:#ff0000"&gt;id&lt;/span&gt; &lt;span style="color:#0000ff"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff"&gt;"website-image-path"&lt;/span&gt; &lt;span style="color:#ff0000"&gt;type&lt;/span&gt; &lt;span style="color:#0000ff"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff"&gt;"hidden"&lt;/span&gt; &lt;span style="color:#ff0000"&gt;value&lt;/span&gt; &lt;span style="color:#0000ff"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff"&gt;'&lt;/span&gt;&lt;span style="background:#ffee62"&gt;&lt;%&lt;/span&gt;=virtualPath + "Content/Images/" &lt;span style="background:#ffee62"&gt;%&gt;&lt;/span&gt;&lt;span style="color:#0000ff"&gt;'&lt;/span&gt; &lt;span style="color:#0000ff"&gt;/&gt;&lt;/span&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
Just place that code somewhere in the body of your master page.
&lt;/p&gt;
&lt;p&gt;
Then access it via JQuery, or standard javascript using something like this:
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:a6c111d5-273d-4979-a992-ad2e0a7295b7" class="wlWriterEditableSmartContent"&gt;
&lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt;
&lt;div style="background: #fff; max-height: 300px; overflow: auto"&gt;
&lt;ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt;
&lt;li&gt;
$(&lt;span style="color:#a31515"&gt;'#website-image-path'&lt;/span&gt;).val();&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
Then finally you’re javascript might look something like this to get at your actual
Image source.
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:0e8449dc-9e89-4ec4-8e2c-2be80e91fffe" class="wlWriterEditableSmartContent"&gt;
&lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt;
&lt;div style="background: #fff; max-height: 300px; overflow: auto"&gt;
&lt;ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; websiteImagePath = $(&lt;span style="color:#a31515"&gt;'#website-image-path'&lt;/span&gt;).val();&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
$(&lt;span style="color:#a31515"&gt;'.ok-button'&lt;/span&gt;).attr(&lt;span style="color:#a31515"&gt;'src'&lt;/span&gt;,
websiteImagePath + &lt;span style="color:#a31515"&gt;'btn_ok.jpg'&lt;/span&gt;);&lt;/li&gt;
&lt;li&gt;
$(&lt;span style="color:#a31515"&gt;'.ok-button'&lt;/span&gt;).hover(&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;function&lt;/span&gt;() { $(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;).attr(&lt;span style="color:#a31515"&gt;'src'&lt;/span&gt;,
websiteImagePath + &lt;span style="color:#a31515"&gt;'btn_okOver.jpg'&lt;/span&gt;); }&lt;/li&gt;
&lt;li&gt;
, &lt;span style="color:#0000ff"&gt;function&lt;/span&gt;() { $(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;).attr(&lt;span style="color:#a31515"&gt;'src'&lt;/span&gt;,
websiteImagePath + &lt;span style="color:#a31515"&gt;'btn_ok.jpg'&lt;/span&gt;); }&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
);&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://blog.davearlin.com/aggbug.ashx?id=915a5fb7-6059-43dd-ace5-635e12347ebb" /&gt;</description>
      <comments>http://blog.davearlin.com/CommentView,guid,915a5fb7-6059-43dd-ace5-635e12347ebb.aspx</comments>
      <category>ASP.Net MVC</category>
      <category>Deployment</category>
      <category>JQuery</category>
    </item>
    <item>
      <trackback:ping>http://blog.davearlin.com/Trackback.aspx?guid=51f1b932-abc5-441c-a251-fd9e76a3130b</trackback:ping>
      <pingback:server>http://blog.davearlin.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.davearlin.com/PermaLink,guid,51f1b932-abc5-441c-a251-fd9e76a3130b.aspx</pingback:target>
      <dc:creator>Dave Arlin</dc:creator>
      <wfw:comment>http://blog.davearlin.com/CommentView,guid,51f1b932-abc5-441c-a251-fd9e76a3130b.aspx</wfw:comment>
      <wfw:commentRss>http://blog.davearlin.com/SyndicationService.asmx/GetEntryCommentsRss?guid=51f1b932-abc5-441c-a251-fd9e76a3130b</wfw:commentRss>
      <title>A Simple Extension of ASP.NET MVC HTMLHelper</title>
      <guid isPermaLink="false">http://blog.davearlin.com/PermaLink,guid,51f1b932-abc5-441c-a251-fd9e76a3130b.aspx</guid>
      <link>http://blog.davearlin.com/2010/05/01/ASimpleExtensionOfASPNETMVCHTMLHelper.aspx</link>
      <pubDate>Sat, 01 May 2010 03:17:20 GMT</pubDate>
      <description>&lt;p&gt;
If you’re an ASP.NET MVC developer, you’ve almost undoubtedly used HTML helpers, or
at least the built in ones.
&lt;/p&gt;
&lt;p&gt;
Here’s are some common ones:
&lt;/p&gt;
&lt;p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:dd5bed22-7ed2-403a-bfb6-3240e8b79eca" class="wlWriterEditableSmartContent"&gt;
&lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt;
&lt;div style="background: #fff; max-height: 300px; overflow: auto"&gt;
&lt;ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px;"&gt;
&lt;li&gt;
&lt;span style="background:#ffee62"&gt;&lt;%&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;Html.DropDownList(&lt;span style="color:#a31515"&gt;"Countries"&lt;/span&gt;,&lt;span style="color:#a31515"&gt;"Choose Country"&lt;/span&gt;)&lt;span style="background:#ffee62"&gt;%&gt;&lt;/span&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&gt;
&lt;p&gt;
This generates a DropDownList populated with the property “Countries” of type “SelectList”
on the ViewData model passed into the view.
&lt;/p&gt;
&lt;p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:51c88f73-5b3f-4406-9cd9-1f530966da05" class="wlWriterEditableSmartContent"&gt;
&lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt;
&lt;div style="background: #fff; max-height: 300px; overflow: auto"&gt;
&lt;ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px;"&gt;
&lt;li&gt;
&lt;span style="background:#ffee62"&gt;&lt;%&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;Html.TextBox(&lt;span style="color:#a31515"&gt;"EntitySearch"&lt;/span&gt;)&lt;span style="background:#ffee62"&gt;%&gt;&lt;/span&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&gt;
&lt;p&gt;
This generates a TextBox bound to the property “EntitySearch” on the ViewData model
passed into the view. Can also be used to populate a property with the same name upon
a form post.
&lt;/p&gt;
&lt;p&gt;
In my current project, I’ve found the need to have many different buttons. These buttons
are custom in the sense that they aren’t the standard HTML buttons, they have CSS
and/or images associated with them. Here’s how I might declare a couple of them right
now:
&lt;/p&gt;
&lt;p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:0b9ca86d-91c9-45c3-8435-2c5673dfd48d" class="wlWriterEditableSmartContent"&gt;
&lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt;
&lt;div style="background: #fff; max-height: 300px; overflow: auto"&gt;
&lt;ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px;"&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;&lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;input&lt;/span&gt; &lt;span style="color:#ff0000"&gt;type&lt;/span&gt; &lt;span style="color:#0000ff"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff"&gt;"image"&lt;/span&gt; &lt;span style="color:#ff0000"&gt;class&lt;/span&gt; &lt;span style="color:#0000ff"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff"&gt;"addnew-button"&lt;/span&gt; &lt;span style="color:#0000ff"&gt;/&gt;&lt;/span&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&gt;
&lt;p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e9bf5f35-42a4-4c41-a11e-a44cddd9e09a" class="wlWriterEditableSmartContent"&gt;
&lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt;
&lt;div style="background: #fff; max-height: 300px; overflow: auto"&gt;
&lt;ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px;"&gt;
&lt;li&gt;
&lt;span style="background:#ffee62"&gt;&lt;%&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;Html.SubmitImage(&lt;span style="color:#a31515"&gt;"Submit"&lt;/span&gt;,&lt;span style="color:#a31515"&gt;"~/images/submit-button.png"&lt;/span&gt;) &lt;span style="background:#ffee62"&gt;%&gt;&lt;/span&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&gt;
&lt;p&gt;
So in both cases, I have to type in a string that kind of defines my button. In the
first example, I have to say my button is an “image” and the class to use to style
the button is called “addnew-button”. 
&lt;/p&gt;
&lt;p&gt;
In the second case, I have to define the actual path to the button. It’d be nice to
have a more common and type safe way to declare all of my buttons, so I’ve created
a Button HtmlHelper method. My goal is to be able to have code in my view like this:
&lt;/p&gt;
&lt;p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e39b116f-0af4-4342-85f3-105801b63541" class="wlWriterEditableSmartContent"&gt;
&lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt;
&lt;div style="background: #fff; max-height: 300px; overflow: auto"&gt;
&lt;ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px;"&gt;
&lt;li&gt;
&lt;span style="background:#ffee62"&gt;&lt;%&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;Html.Button(&lt;span style="color:#2b91af"&gt;ButtonHtmlHelper&lt;/span&gt;.&lt;span style="color:#2b91af"&gt;ButtonType&lt;/span&gt;.AddNew) &lt;span style="background:#ffee62"&gt;%&gt;&lt;/span&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&gt;
&lt;p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:864e5d23-a0c8-4be6-890a-2fac78aea2e9" class="wlWriterEditableSmartContent"&gt;
&lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt;
&lt;div style="background: #fff; max-height: 300px; overflow: auto"&gt;
&lt;ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px;"&gt;
&lt;li&gt;
&lt;span style="background:#ffee62"&gt;&lt;%&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;Html.Button(&lt;span style="color:#2b91af"&gt;ButtonHtmlHelper&lt;/span&gt;.&lt;span style="color:#2b91af"&gt;ButtonType&lt;/span&gt;.Submit) &lt;span style="background:#ffee62"&gt;%&gt;&lt;/span&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
To do this, I created an extension method on the HtmlHelper class called button. This
extension method can handle all my button types and has some overloads to accept a
client id and any additional custom styling.
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c011331e-a3bc-4b87-b9e4-173928a1ff9c" class="wlWriterEditableSmartContent"&gt;
&lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt;
&lt;div style="background: #fff; max-height: 500px; overflow: auto"&gt;
&lt;ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;namespace&lt;/span&gt; MyProject.HtmlHelpers&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
{&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ButtonHtmlHelper&lt;/span&gt;
&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
{&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;enum&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ButtonType&lt;/span&gt; {
AddNew, Remove, Ok, Cancel, Submit, Save, Search, Clear, Continue, Active, Inactive
}&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; Button(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt; &lt;span style="color:#2b91af"&gt;HtmlHelper&lt;/span&gt; htmlHelper, &lt;span style="color:#2b91af"&gt;ButtonType&lt;/span&gt; buttonType)&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
{&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;return&lt;/span&gt; Button(htmlHelper, buttonType, &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;, &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;);&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
}&lt;/li&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; Button(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt; &lt;span style="color:#2b91af"&gt;HtmlHelper&lt;/span&gt; htmlHelper, &lt;span style="color:#2b91af"&gt;ButtonType&lt;/span&gt; buttonType, &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; id)&lt;/li&gt;
&lt;li&gt;
{&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;return&lt;/span&gt; Button(htmlHelper, buttonType, id, &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;);&lt;/li&gt;
&lt;li&gt;
}&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; Button(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt; &lt;span style="color:#2b91af"&gt;HtmlHelper&lt;/span&gt; htmlHelper, &lt;span style="color:#2b91af"&gt;ButtonType&lt;/span&gt; buttonType, &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; id, &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; inlineCSS)&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
{&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#2b91af"&gt;StringBuilder&lt;/span&gt; addNewButtonHTML = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;StringBuilder&lt;/span&gt;();&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
addNewButtonHTML.Append(&lt;span style="color:#a31515"&gt;@"&lt;input class= """&lt;/span&gt;);
&lt;/li&gt;
&lt;li&gt;
addNewButtonHTML.Append(GetButtonTypeCSSClassPrefix(buttonType));&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
addNewButtonHTML.Append(&lt;span style="color:#a31515"&gt;@"-button"" type = ""image"" "&lt;/span&gt;);&lt;/li&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (!&lt;span style="color:#0000ff"&gt;string&lt;/span&gt;.IsNullOrEmpty(id))&lt;/li&gt;
&lt;li&gt;
addNewButtonHTML.AppendFormat(&lt;span style="color:#a31515"&gt;@"id = ""{0}"" "&lt;/span&gt;,
id);&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (!&lt;span style="color:#0000ff"&gt;string&lt;/span&gt;.IsNullOrEmpty(inlineCSS))&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
addNewButtonHTML.AppendFormat(&lt;span style="color:#a31515"&gt;@"style = ""{0}"" "&lt;/span&gt;,
inlineCSS);&lt;/li&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
addNewButtonHTML.Append(&lt;span style="color:#a31515"&gt;"/&gt;"&lt;/span&gt;);&lt;/li&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;return&lt;/span&gt; addNewButtonHTML.ToString();&lt;/li&gt;
&lt;li&gt;
}&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; GetButtonTypeCSSClassPrefix(&lt;span style="color:#2b91af"&gt;ButtonType&lt;/span&gt; buttonType)&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
{&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;switch&lt;/span&gt; (buttonType)&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
{&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;case&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ButtonType&lt;/span&gt;.AddNew:&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;return&lt;/span&gt; &lt;span style="color:#a31515"&gt;"addnew"&lt;/span&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;case&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ButtonType&lt;/span&gt;.Cancel:&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;return&lt;/span&gt; &lt;span style="color:#a31515"&gt;"cancel"&lt;/span&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;case&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ButtonType&lt;/span&gt;.Ok:&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;return&lt;/span&gt; &lt;span style="color:#a31515"&gt;"ok"&lt;/span&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;case&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ButtonType&lt;/span&gt;.Remove:&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;return&lt;/span&gt; &lt;span style="color:#a31515"&gt;"remove"&lt;/span&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;case&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ButtonType&lt;/span&gt;.Submit:&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;return&lt;/span&gt; &lt;span style="color:#a31515"&gt;"submit"&lt;/span&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;case&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ButtonType&lt;/span&gt;.Save:&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;return&lt;/span&gt; &lt;span style="color:#a31515"&gt;"save"&lt;/span&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;case&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ButtonType&lt;/span&gt;.Search:&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;return&lt;/span&gt; &lt;span style="color:#a31515"&gt;"search"&lt;/span&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;case&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ButtonType&lt;/span&gt;.Clear:&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;return&lt;/span&gt; &lt;span style="color:#a31515"&gt;"clear"&lt;/span&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;case&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ButtonType&lt;/span&gt;.Continue:&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;return&lt;/span&gt; &lt;span style="color:#a31515"&gt;"continue"&lt;/span&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;case&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ButtonType&lt;/span&gt;.Active:&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;return&lt;/span&gt; &lt;span style="color:#a31515"&gt;"active"&lt;/span&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;case&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ButtonType&lt;/span&gt;.Inactive:&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;return&lt;/span&gt; &lt;span style="color:#a31515"&gt;"inactive"&lt;/span&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;default&lt;/span&gt;:&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ArgumentException&lt;/span&gt;(&lt;span style="color:#0000ff"&gt;string&lt;/span&gt;.Format(&lt;span style="color:#a31515"&gt;"Unknown
ButtonType of {0} was passed in."&lt;/span&gt;, buttonType));&lt;/li&gt;
&lt;li&gt;
}&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
}&lt;/li&gt;
&lt;li&gt;
}&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
}&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
That’s all! Just make sure you either add a using statement on the view to the namespace
where the HTMLHelper is, or put it in the web.config like I did in my project. (line
10)
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:9551f6e6-3c3d-4700-972a-a56682c04e0a" class="wlWriterEditableSmartContent"&gt;
&lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt;
&lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt;
&lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px; white-space: nowrap"&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;&lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;namespaces&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&gt;&lt;/span&gt;
&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;&lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;add&lt;/span&gt;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;"&lt;span style="color:#0000ff"&gt;System.Web.Mvc&lt;/span&gt;"&lt;span style="color:#0000ff"&gt;/&gt;&lt;/span&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;&lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;add&lt;/span&gt;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;"&lt;span style="color:#0000ff"&gt;System.Web.Mvc.Ajax&lt;/span&gt;"&lt;span style="color:#0000ff"&gt;/&gt;&lt;/span&gt;
&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;&lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;add&lt;/span&gt;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;"&lt;span style="color:#0000ff"&gt;System.Web.Mvc.Html&lt;/span&gt;"&lt;span style="color:#0000ff"&gt;/&gt;&lt;/span&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;&lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;add&lt;/span&gt;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;"&lt;span style="color:#0000ff"&gt;System.Web.Routing&lt;/span&gt;"&lt;span style="color:#0000ff"&gt;/&gt;&lt;/span&gt;
&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;&lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;add&lt;/span&gt;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;"&lt;span style="color:#0000ff"&gt;System.Linq&lt;/span&gt;"&lt;span style="color:#0000ff"&gt;/&gt;&lt;/span&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;&lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;add&lt;/span&gt;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;"&lt;span style="color:#0000ff"&gt;System.Collections.Generic&lt;/span&gt;"&lt;span style="color:#0000ff"&gt;/&gt;&lt;/span&gt;
&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;&lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;add&lt;/span&gt;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;"&lt;span style="color:#0000ff"&gt;Microsoft.Web.Mvc&lt;/span&gt;"&lt;span style="color:#0000ff"&gt;/&gt;&lt;/span&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;&lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;add&lt;/span&gt;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;"&lt;span style="color:#0000ff"&gt;xVal.Html&lt;/span&gt;"&lt;span style="color:#0000ff"&gt;/&gt;&lt;/span&gt;
&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;&lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;add&lt;/span&gt;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;"&lt;span style="color:#0000ff"&gt;MyProject.HtmlHelpers&lt;/span&gt;"&lt;span style="color:#0000ff"&gt;/&gt;&lt;/span&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;namespaces&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&gt;&lt;/span&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://blog.davearlin.com/aggbug.ashx?id=51f1b932-abc5-441c-a251-fd9e76a3130b" /&gt;</description>
      <comments>http://blog.davearlin.com/CommentView,guid,51f1b932-abc5-441c-a251-fd9e76a3130b.aspx</comments>
      <category>ASP.Net MVC</category>
      <category>HTMLHelper</category>
    </item>
    <item>
      <trackback:ping>http://blog.davearlin.com/Trackback.aspx?guid=9b5c9cd4-190d-48fe-af70-2d269636653c</trackback:ping>
      <pingback:server>http://blog.davearlin.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.davearlin.com/PermaLink,guid,9b5c9cd4-190d-48fe-af70-2d269636653c.aspx</pingback:target>
      <dc:creator>Dave Arlin</dc:creator>
      <wfw:comment>http://blog.davearlin.com/CommentView,guid,9b5c9cd4-190d-48fe-af70-2d269636653c.aspx</wfw:comment>
      <wfw:commentRss>http://blog.davearlin.com/SyndicationService.asmx/GetEntryCommentsRss?guid=9b5c9cd4-190d-48fe-af70-2d269636653c</wfw:commentRss>
      <title>JSON to .NET Object Using JQuery and IModelBinder in ASP.NET MVC</title>
      <guid isPermaLink="false">http://blog.davearlin.com/PermaLink,guid,9b5c9cd4-190d-48fe-af70-2d269636653c.aspx</guid>
      <link>http://blog.davearlin.com/2010/04/30/JSONToNETObjectUsingJQueryAndIModelBinderInASPNETMVC.aspx</link>
      <pubDate>Fri, 30 Apr 2010 03:15:59 GMT</pubDate>
      <description>&lt;p&gt;
Ok, so I have a pretty basic web page that displays a paginated grid of a collection
of my business objects. This grid gets displayed when I perform a simple string search
on let’s say my entity name. Of course, everyone hates dealing with full postbacks,
so I want to perform this search via javascript, or more specifically via an AJAX
call. 
&lt;/p&gt;
&lt;p&gt;
Using the typical form post method common in many ASP.NET MVC applications doesn’t
really work in my scenario because I’m not mapping all the necessary fields upon landing
on my page. 
&lt;/p&gt;
&lt;p&gt;
I know I want to use JQuery’s AJAX capabilities to post my search string information,
but I couldn’t figure out a way to do it without passing in an “ugly” set of query
strings and having to accept 3 parameters in my Controller Action method. 
&lt;/p&gt;
&lt;p&gt;
Basically I didn’t want to have to have something like this:
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:678ff489-ffa0-412f-8c3b-5be55d5d0c61" class="wlWriterEditableSmartContent"&gt;
&lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt;
&lt;div style="background: #fff; max-height: 300px; overflow: auto"&gt;
&lt;ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt;
&lt;li&gt;
[&lt;span style="color:#2b91af"&gt;NoCache&lt;/span&gt;][&lt;span style="color:#2b91af"&gt;AcceptVerbs&lt;/span&gt;(&lt;span style="color:#2b91af"&gt;HttpVerbs&lt;/span&gt;.Post)]&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ActionResult&lt;/span&gt; SearchEntities(&lt;span style="color:#0000ff"&gt;string&lt;/span&gt; searchValue, &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; pageNumber, &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; itemsPerPage)&lt;/li&gt;
&lt;li&gt;
{&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; allSearchResults = _entityAdministrationService.SearchEntities(searchValue);&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; searchResultsForPage = allSearchResults.Skip((pageNumber
- 1) * itemsPerPage).Take(itemsPerPage);&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;return&lt;/span&gt; Json(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;EntitySearchResultsInput&lt;/span&gt;
&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
{&lt;/li&gt;
&lt;li&gt;
Entities = searchResultsForPage,&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
ItemsPerPage = itemsPerPage,&lt;/li&gt;
&lt;li&gt;
PageNumber = pageNumber,&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
TotalResults = allSearchResults.Count()&lt;/li&gt;
&lt;li&gt;
});&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
}&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
(oh by the way, going forward in this post, ignore my custom “NoCache” attribute in
any of my code snippets. It’s not related to this post, but I use it all the time
now because of the automatic post caching feature in IE).
&lt;/p&gt;
&lt;p&gt;
Ok, so if you look at the method signature, it really doesn’t look THAT bad right?
Well it’s not bad, but the problem is that I’m sure I’ll have other pages that need
to perform AJAX calls that will pass in other sets of parameters. I want one to have
a nice POCO that I can work with that gets “automatically” mapped for me. 
&lt;/p&gt;
&lt;p&gt;
I want something like this:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:3db425a3-c600-4170-ad9a-ec777f234678" class="wlWriterEditableSmartContent"&gt;
&lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt;
&lt;div style="background: #fff; max-height: 300px; overflow: auto"&gt;
&lt;ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt;
&lt;li&gt;
[&lt;span style="color:#2b91af"&gt;NoCache&lt;/span&gt;][&lt;span style="color:#2b91af"&gt;AcceptVerbs&lt;/span&gt;(&lt;span style="color:#2b91af"&gt;HttpVerbs&lt;/span&gt;.Post)]&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ActionResult&lt;/span&gt; SearchEntities(&lt;span style="color:#2b91af"&gt;EntitySearchOutput&lt;/span&gt; entitySearchOutput)&lt;/li&gt;
&lt;li&gt;
{&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; allSearchResults = _entityAdministrationService.SearchEntities(entitySearchOutput.SearchValue);&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; searchResultsForPage = allSearchResults.Skip((entitySearchOutput.PageNumber
- 1) * entitySearchOutput.ItemsPerPage).Take(entitySearchOutput.ItemsPerPage);&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;return&lt;/span&gt; Json(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;EntitySearchResultsInput&lt;/span&gt;
&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
{&lt;/li&gt;
&lt;li&gt;
Entities = searchResultsForPage,&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
ItemsPerPage = entitySearchOutput.ItemsPerPage,&lt;/li&gt;
&lt;li&gt;
PageNumber = entitySearchOutput.PageNumber,&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
TotalResults = allSearchResults.Count()&lt;/li&gt;
&lt;li&gt;
});&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
}&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
Where the definition of EntitySearchOutput is:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:67f16ba9-013c-4c10-895a-1d0b9cb35374" class="wlWriterEditableSmartContent"&gt;
&lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt;
&lt;div style="background: #fff; max-height: 300px; overflow: auto"&gt;
&lt;ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;EntitySearchOutput&lt;/span&gt;
&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
{&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; SearchValue
{ &lt;span style="color:#0000ff"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff"&gt;set&lt;/span&gt;;
}&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; PageNumber
{ &lt;span style="color:#0000ff"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff"&gt;set&lt;/span&gt;;
}&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; ItemsPerPage
{ &lt;span style="color:#0000ff"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff"&gt;set&lt;/span&gt;;
}&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
}&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
So, making use of Google’s &lt;a href="http://code.google.com/p/jquery-json/"&gt;jquery.json
library&lt;/a&gt;, and javascript objects, I’m able to write the following javascript code
that calls my “SearchEntities” controller action.
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:3d5ce4f5-729d-470e-9d40-f71b9c21a6db" class="wlWriterEditableSmartContent"&gt;
&lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt;
&lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt;
&lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; searchValue = GetSearchValue();&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; currentPageNumber = GetCurrentPageNumber();&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; itemsPerPage = 20;&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; searchData = {&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#a31515"&gt;'SearchValue'&lt;/span&gt;: searchValue,&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#a31515"&gt;'PageNumber'&lt;/span&gt;: currentPageNumber,&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#a31515"&gt;'ItemsPerPage'&lt;/span&gt;: itemsPerPage&lt;/li&gt;
&lt;li&gt;
};&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; searchDataJSON = $.toJSON(searchData);&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
searchDataJSON = searchDataJSON.replace(/&lt;span style="color:#0000ff"&gt;null&lt;/span&gt;/g, &lt;span style="color:#a31515"&gt;''&lt;/span&gt;);&lt;/li&gt;
&lt;li&gt;
$.post(&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#a31515"&gt;'../ApplicationManagement/SearchEntities'&lt;/span&gt;,&lt;/li&gt;
&lt;li&gt;
{ EntitySearchOutput: searchDataJSON },&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;function&lt;/span&gt;(jsonResult) {&lt;/li&gt;
&lt;li&gt;
DisplaySearchResults(jsonResult);&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
}, &lt;span style="color:#a31515"&gt;"json"&lt;/span&gt;);&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
So what this actually does is builds out my “searchData” javascript object and then
runs through the “$.toJSON” function to convert the javascript object to a JSON object
called “searchDataJSON”. This in turn gets passed to my “SearchEntities” method as
a JSON string. As a side note, line 12 is necessary for IE so that the JSON string
gets created in a way that can be de-serialized by .NETs JavascriptSerializer as you’ll
see soon.
&lt;/p&gt;
&lt;p&gt;
So how does the application know to convert what I passed in javascript to my EntitySearchOutput
class? That’s where an IModelBinder implementation comes into play.
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ed1f2cbf-8251-4919-81a6-0a89b376d3e5" class="wlWriterEditableSmartContent"&gt;
&lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt;
&lt;div style="background: #fff; max-height: 500px; overflow: auto"&gt;
&lt;ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;JsonModelBinder&lt;/span&gt;
&lt;T&gt;
:&lt;span style="color:#2b91af"&gt;IModelBinder&lt;/span&gt; &lt;span style="color:#0000ff"&gt;where&lt;/span&gt; T:&lt;span style="color:#0000ff"&gt;class&lt;/span&gt;
&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
{&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;#region&lt;/span&gt; IModelBinder Members&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;object&lt;/span&gt; BindModel(&lt;span style="color:#2b91af"&gt;ControllerContext&lt;/span&gt; controllerContext, &lt;span style="color:#2b91af"&gt;ModelBindingContext&lt;/span&gt; bindingContext)&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
{&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;string&lt;/span&gt; jsonString = controllerContext.RequestContext.HttpContext.Request.Params[0];&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#2b91af"&gt;JavaScriptSerializer&lt;/span&gt; serializer = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;JavaScriptSerializer&lt;/span&gt;();&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; result = serializer.DeserializeObject(jsonString);&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;return&lt;/span&gt; serializer.ConvertToType&lt;T&gt;
(result);
&lt;/li&gt;
&lt;li&gt;
}&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;#endregion&lt;/span&gt;
&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
}&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
As you can see, this JsonModelBinder class does not say anything about EntitySearchOutput.
As a matter of fact, it is a generic class which means, yes, it can be used for any
class provided it is Javascript serializable. So you can do this with deep complex
arrays. The one method you must implement is called “BindModel” which gives you a
reference to the HttpContext. The json String exists in the HttpContext.Request parameter.
Simple use the JavaScriptSerializer framework class and deserialize to the generic
type T and return the results.
&lt;/p&gt;
&lt;p&gt;
The final piece of this to tie in the model binder comes in the Global.asax.cs 
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:7e48d0d2-1a18-401e-8f97-6774a28f6e00" class="wlWriterEditableSmartContent"&gt;
&lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt;
&lt;div style="background: #fff; max-height: 300px; overflow: auto"&gt;
&lt;ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Application_Start()&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
{&lt;/li&gt;
&lt;li&gt;
RegisterRoutes(&lt;span style="color:#2b91af"&gt;RouteTable&lt;/span&gt;.Routes);&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
SetupIoCContainer(); 
&lt;/li&gt;
&lt;li&gt;
SetupModelBinders(); 
&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
}&lt;/li&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; SetupModelBinders()&lt;/li&gt;
&lt;li&gt;
{&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#2b91af"&gt;ModelBinders&lt;/span&gt;.Binders.Add(&lt;span style="color:#0000ff"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af"&gt;EntitySearchOutput&lt;/span&gt;), &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;JsonModelBinder&lt;/span&gt;&lt;&lt;span style="color:#2b91af"&gt;EntitySearchOutput&gt;&gt;());&lt;/li&gt;
&lt;li&gt;
}&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
The “SetupModelBinders” method makes a call that tells the MVC Framework to always
using the JsonModelBinder class to create an EntitySearchOutput object when it is
specified as a parameter of a controller action. 
&lt;/p&gt;
&lt;p&gt;
Now the controller action will work as expected:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:34d13e1a-27ce-4db2-8440-c67308133009" class="wlWriterEditableSmartContent"&gt;
&lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt;
&lt;div style="background: #fff; max-height: 300px; overflow: auto"&gt;
&lt;ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt;
&lt;li&gt;
[&lt;span style="color:#2b91af"&gt;NoCache&lt;/span&gt;][&lt;span style="color:#2b91af"&gt;AcceptVerbs&lt;/span&gt;(&lt;span style="color:#2b91af"&gt;HttpVerbs&lt;/span&gt;.Post)]&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ActionResult&lt;/span&gt; SearchEntities(&lt;span style="color:#2b91af"&gt;EntitySearchOutput&lt;/span&gt; entitySearchOutput)&lt;/li&gt;
&lt;li&gt;
{&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; allSearchResults = _entityAdministrationService.SearchEntities(entitySearchOutput.SearchValue);&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; searchResultsForPage = allSearchResults.Skip((entitySearchOutput.PageNumber
- 1) * entitySearchOutput.ItemsPerPage).Take(entitySearchOutput.ItemsPerPage);&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;return&lt;/span&gt; Json(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;EntitySearchResultsInput&lt;/span&gt;
&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
{&lt;/li&gt;
&lt;li&gt;
Entities = searchResultsForPage,&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
ItemsPerPage = entitySearchOutput.ItemsPerPage,&lt;/li&gt;
&lt;li&gt;
PageNumber = entitySearchOutput.PageNumber,&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
TotalResults = allSearchResults.Count()&lt;/li&gt;
&lt;li&gt;
});&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
}&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
The final part of this method returns back JSON to our javascript AJAX call, it JSON
serializes an “EntitySearchResultsInput” class defined here:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ec4dc431-f999-4293-bced-8510ef0b1b0e" class="wlWriterEditableSmartContent"&gt;
&lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt;
&lt;div style="background: #fff; max-height: 300px; overflow: auto"&gt;
&lt;ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;EntitySearchResultsInput&lt;/span&gt;
&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
{&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af"&gt;IEnumerable&lt;/span&gt;&lt;&lt;span style="color:#2b91af"&gt;Entity&gt;&gt;
Entities { &lt;span style="color:#0000ff"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff"&gt;set&lt;/span&gt;;
}&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; ItemsPerPage
{ &lt;span style="color:#0000ff"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff"&gt;set&lt;/span&gt;;
}&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; PageNumber
{ &lt;span style="color:#0000ff"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff"&gt;set&lt;/span&gt;;
}&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; TotalResults
{ &lt;span style="color:#0000ff"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff"&gt;set&lt;/span&gt;;
}&lt;/li&gt;
&lt;li&gt;
}&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
So that the following “jsonResult” object is populated on line 17 below:
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ef90e839-3e93-4ca8-979b-ab5b8ae8280f" class="wlWriterEditableSmartContent"&gt;
&lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt;
&lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt;
&lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; searchValue = GetSearchValue();&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; currentPageNumber = GetCurrentPageNumber();&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; itemsPerPage = 20;&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; searchData = {&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#a31515"&gt;'SearchValue'&lt;/span&gt;: searchValue,&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#a31515"&gt;'PageNumber'&lt;/span&gt;: currentPageNumber,&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#a31515"&gt;'ItemsPerPage'&lt;/span&gt;: itemsPerPage&lt;/li&gt;
&lt;li&gt;
};&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; searchDataJSON = $.toJSON(searchData);&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
searchDataJSON = searchDataJSON.replace(/&lt;span style="color:#0000ff"&gt;null&lt;/span&gt;/g, &lt;span style="color:#a31515"&gt;''&lt;/span&gt;);&lt;/li&gt;
&lt;li&gt;
$.post(&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#a31515"&gt;'../ApplicationManagement/SearchEntities'&lt;/span&gt;,&lt;/li&gt;
&lt;li&gt;
{ EntitySearchOutput: searchDataJSON },&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;function&lt;/span&gt;(jsonResult) {&lt;/li&gt;
&lt;li&gt;
DisplaySearchResults(jsonResult);&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
}, &lt;span style="color:#a31515"&gt;"json"&lt;/span&gt;);&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
You can access properties on “jsonResult” simply by typing in: jsonResult.TotalResults
or jsonResult.Entities[0].Name
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
So that’s an example of one implementation of IModelBinder. You might find other things
you want to do with other types of ModelBinders. I actually created another one for
a specific scenario I had with arrays of integers. Here is that ModelBinder. I’ll
leave it to you to see how you might use it.
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e564b8e0-7677-4e8f-9967-06b76e594df1" class="wlWriterEditableSmartContent"&gt;
&lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt;
&lt;div style="background: #fff; max-height: 300px; overflow: auto"&gt;
&lt;ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;CommaSeperatedStringToIntegerEnumModelBinder&lt;/span&gt; : &lt;span style="color:#2b91af"&gt;IModelBinder&lt;/span&gt;
&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
{&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;#region&lt;/span&gt; IModelBinder Members&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;object&lt;/span&gt; BindModel(&lt;span style="color:#2b91af"&gt;ControllerContext&lt;/span&gt; controllerContext, &lt;span style="color:#2b91af"&gt;ModelBindingContext&lt;/span&gt; bindingContext)&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
{&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;string&lt;/span&gt; commaSeperatedString = controllerContext.RequestContext.HttpContext.Request.Params[&lt;span style="color:#a31515"&gt;"Ids"&lt;/span&gt;];&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;string&lt;/span&gt;[] intStringArray = commaSeperatedString.Split(&lt;span style="color:#a31515"&gt;','&lt;/span&gt;);&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#2b91af"&gt;IEnumerable&lt;/span&gt;&lt;&lt;span style="color:#0000ff"&gt;int&gt;&lt;&lt;span style="color:#0000ff"&gt;&gt;
intArray = intStringArray.ToList().ConvertAllint&gt;&gt;(&lt;span style="color:#0000ff"&gt;delegate&lt;/span&gt;(&lt;span style="color:#0000ff"&gt;string&lt;/span&gt; str)
{ &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff"&gt;int&lt;/span&gt;.Parse(str);
});&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;return&lt;/span&gt; intArray;&lt;/li&gt;
&lt;li&gt;
}&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;#endregion&lt;/span&gt;
&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
}&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://blog.davearlin.com/aggbug.ashx?id=9b5c9cd4-190d-48fe-af70-2d269636653c" /&gt;</description>
      <comments>http://blog.davearlin.com/CommentView,guid,9b5c9cd4-190d-48fe-af70-2d269636653c.aspx</comments>
      <category>ASP.Net MVC</category>
      <category>IModelBinder</category>
      <category>Inversion of Control (IoC)</category>
      <category>JQuery</category>
    </item>
    <item>
      <trackback:ping>http://blog.davearlin.com/Trackback.aspx?guid=0c2deede-9c40-423d-b630-a550f2c6b548</trackback:ping>
      <pingback:server>http://blog.davearlin.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.davearlin.com/PermaLink,guid,0c2deede-9c40-423d-b630-a550f2c6b548.aspx</pingback:target>
      <dc:creator>Dave Arlin</dc:creator>
      <wfw:comment>http://blog.davearlin.com/CommentView,guid,0c2deede-9c40-423d-b630-a550f2c6b548.aspx</wfw:comment>
      <wfw:commentRss>http://blog.davearlin.com/SyndicationService.asmx/GetEntryCommentsRss?guid=0c2deede-9c40-423d-b630-a550f2c6b548</wfw:commentRss>
      <title>Useful Usage of Microsoft Unity Application Block with ASP.Net MVC</title>
      <guid isPermaLink="false">http://blog.davearlin.com/PermaLink,guid,0c2deede-9c40-423d-b630-a550f2c6b548.aspx</guid>
      <link>http://blog.davearlin.com/2009/12/25/UsefulUsageOfMicrosoftUnityApplicationBlockWithASPNetMVC.aspx</link>
      <pubDate>Fri, 25 Dec 2009 20:11:14 GMT</pubDate>
      <description>&lt;h1&gt;
&lt;/h1&gt;
&lt;h4&gt;Testing is Always Number One
&lt;/h4&gt;
&lt;p&gt;
If you read any book on why using an &lt;a href="http://en.wikipedia.org/wiki/Inversion_of_control"&gt;IoC
(Inversion of Control)&lt;/a&gt; approach to your website using tools like Microsoft’s &lt;a href="http://msdn.microsoft.com/en-us/library/cc468366.aspx"&gt;Unity
Application Block&lt;/a&gt; and a Mocking Framework like &lt;a href="http://www.ayende.com/"&gt;Rhino
Mocks&lt;/a&gt;, the number one reason is going to be writing solid unit tests, usually
using a practice like TDD. While I have no reason to doubt that that should be the
number one reason, I like to always try and find more “live and in the moment” scenarios
for using these tools. 
&lt;/p&gt;
&lt;p&gt;
Here’s a very common testing scenario and implementation for testing that the &lt;a href="http://google.com/analytics"&gt;Google
Analytics&lt;/a&gt; javascript is only appearing in my site if I am in a Production environment.
This makes also makes use of &lt;a href="http://www.codeplex.com/MVCContrib"&gt;MvcContrib&lt;/a&gt;.
&lt;/p&gt;
&lt;table border="1" cellspacing="0" cellpadding="2" width="715"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td valign="top" width="713"&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;using &lt;/span&gt;System; &lt;span style="color: blue"&gt;using &lt;/span&gt;System.Collections.Generic; &lt;span style="color: blue"&gt;using &lt;/span&gt;System.Linq; &lt;span style="color: blue"&gt;using &lt;/span&gt;System.Text; &lt;span style="color: blue"&gt;using &lt;/span&gt;System.Web.Mvc; &lt;span style="color: blue"&gt;using &lt;/span&gt;Microsoft.VisualStudio.TestTools.UnitTesting; &lt;span style="color: blue"&gt;using &lt;/span&gt;MyWebSite.Web; &lt;span style="color: blue"&gt;using &lt;/span&gt;MyWebSite.Web.Controllers; &lt;span style="color: blue"&gt;using &lt;/span&gt;MyWebSite.Services; &lt;span style="color: blue"&gt;using &lt;/span&gt;Rhino.Mocks; &lt;span style="color: blue"&gt;using &lt;/span&gt;MvcContrib.TestHelper;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public
class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;HomeControllerTest &lt;/span&gt;{ &lt;span style="color: #2b91af"&gt;IAppConfig &lt;/span&gt;_appConfig; &lt;span style="color: #2b91af"&gt;IEmailService &lt;/span&gt;_emailService; &lt;span style="color: #2b91af"&gt;HomeController &lt;/span&gt;_homeController;
[&lt;span style="color: #2b91af"&gt;TestInitialize&lt;/span&gt;] &lt;span style="color: blue"&gt;public
void &lt;/span&gt;Init() { _appConfig = &lt;span style="color: #2b91af"&gt;MockRepository&lt;/span&gt;&lt;&lt;span style="color: #2b91af"&gt;.GenerateStubIAppConfig&gt;&gt;();
_emailService = &lt;span style="color: #2b91af"&gt;MockRepository&lt;/span&gt;&lt;&lt;span style="color: #2b91af"&gt;.GenerateStubIEmailService&gt;&gt;();
_homeController = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;HomeController&lt;/span&gt;(_appConfig,
_emailService); &lt;span style="color: #2b91af"&gt;TestControllerBuilder &lt;/span&gt;builder
= &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;TestControllerBuilder&lt;/span&gt;();
builder.InitializeController(_homeController); } [&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;] &lt;span style="color: blue"&gt;public
void &lt;/span&gt;Analytics_NotProduction() { &lt;span style="color: green"&gt;// Arrange &lt;/span&gt;_appConfig.Stub(a
=&gt; a.Environment).Return(&lt;span style="color: #a31515"&gt;"Staging"&lt;/span&gt;); &lt;span style="color: green"&gt;//
Act &lt;/span&gt;&lt;span style="color: #2b91af"&gt;EmptyResult &lt;/span&gt;result = _homeController.Analytics() &lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: #2b91af"&gt;EmptyResult&lt;/span&gt;; &lt;span style="color: green"&gt;//
Assert &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.IsNotNull(result); } [&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;] &lt;span style="color: blue"&gt;public
void &lt;/span&gt;Analytics_Production() { &lt;span style="color: green"&gt;// Arrange &lt;/span&gt;_appConfig.Stub(a
=&gt; a.Environment).Return(&lt;span style="color: #a31515"&gt;"Production"&lt;/span&gt;); &lt;span style="color: green"&gt;//
Act &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ViewResult &lt;/span&gt;result = _homeController.Analytics() &lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ViewResult&lt;/span&gt;; &lt;span style="color: green"&gt;//
Assert &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.IsNotNull(result); } }&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
So here, my HomeController class’ constructor needs an instance of both IEmailService
and IAppConfig. Rhino Mocks creates a mock of both of those interfaces and additionally
sets the “Environment” property of the mocked IAppConfig instance to either “Staging”
or “Production” depending on my test. 
&lt;/p&gt;
&lt;p&gt;
In the “Analytics_NotProduction” test, my test passes if an “EmptyResult” instance
is returned by my HomeController’s Analytics method meaning that no View is generated
with any markup or javascript of any kind. The “Analytics_Production” test passes
if the Analytics method returns an actual ViewResult which presumably generates the
javascript I need for Google Analytics.
&lt;/p&gt;
&lt;p&gt;
Again, this is just your typical unit test scenario. This is great, you should do
this. I should do this more than I do right now with my projects. That’s another story.
Here’s another quick and easy (and also immediately useful) scenario I’ve come up
with for using IoC.
&lt;/p&gt;
&lt;h5&gt;
&lt;/h5&gt;
&lt;h4&gt;Shared Hosting Limitations with a Staging and Production Environment
&lt;/h4&gt;
&lt;p&gt;
So recently, I’ve begun working on my wife’s business website, &lt;a href="http://jamieldesigns.com"&gt;Jamie
Lynn Designs&lt;/a&gt;. This website is currently hosted using &lt;a href="http://www.softsyshosting.com/"&gt;Softsys
Hosting’s Shared Hosting&lt;/a&gt;. &lt;font color="#006200"&gt;/* End shameful social marketting
plug */&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
On this website, random quotes are displayed from my wife’s happy customers. Every
time you load up a page, another random quote displays. These quotes are stored in
a SQL Database. 
&lt;/p&gt;
&lt;p&gt;
Snippet from &lt;strong&gt;HomeController.cs&lt;/strong&gt;
&lt;/p&gt;
&lt;table border="1" cellspacing="0" cellpadding="2" width="730"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td valign="top" width="728"&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt; private &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IAppConfig &lt;/span&gt;_appConfig; &lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IClientQuoteService &lt;/span&gt;_clientQuoteService; &lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IContactSubmissionService &lt;/span&gt;_contactSubmissionService; &lt;span style="color: blue"&gt;public &lt;/span&gt;HomeController(&lt;span style="color: #2b91af"&gt;IAppConfig &lt;/span&gt;appConfig, &lt;span style="color: #2b91af"&gt;IContactSubmissionService &lt;/span&gt;contactSubmissionService, 
&lt;br&gt;
&lt;span style="color: #2b91af"&gt;IClientQuoteService &lt;/span&gt;clientQuoteService) { _appConfig
= appConfig; _contactSubmissionService = contactSubmissionService; _clientQuoteService
= clientQuoteService; }&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ActionResult &lt;/span&gt;RandomQuote()
{ &lt;span style="color: #2b91af"&gt;ClientQuote &lt;/span&gt;clientQuote = _clientQuoteService.GetRandomClientQuote(); &lt;span style="color: blue"&gt;return &lt;/span&gt;View(clientQuote);
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
&lt;span style="color: blue"&gt; &lt;/span&gt;As you see, in the “RandomQuote” method, there is
a call to the IClientQuoteService’s “GetRandomClientQuote” method. Using Unity, a
concrete implementation of this interface called “ClientQuoteService” is used.
&lt;/p&gt;
&lt;p&gt;
Snippet from &lt;strong&gt;ClientQuoteService.cs&lt;/strong&gt;
&lt;/p&gt;
&lt;table border="1" cellspacing="0" cellpadding="2" width="708"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td valign="top" width="706"&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ClientQuoteService&lt;/span&gt;:&lt;span style="color: #2b91af"&gt;IClientQuoteService &lt;/span&gt;{ &lt;span style="color: #2b91af"&gt;IRepository &lt;/span&gt;_repository; &lt;span style="color: blue"&gt;public &lt;/span&gt;ClientQuoteService(&lt;span style="color: #2b91af"&gt;IRepository &lt;/span&gt;repository)
{ _repository = repository; } &lt;span style="color: blue"&gt;#region &lt;/span&gt;IClientQuoteService
Members &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ClientQuote &lt;/span&gt;GetRandomClientQuote()
{ &lt;span style="color: blue"&gt;int &lt;/span&gt;&lt;&lt;span style="color: #2b91af"&gt;clientQuoteCount
= _repository.GetAllClientQuote&gt;&gt;().Count(); &lt;span style="color: blue"&gt;int &lt;/span&gt;clientQuoteIndex
= &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Random&lt;/span&gt;().Next(clientQuoteCount); &lt;span style="color: blue"&gt;return &lt;/span&gt;&lt;&lt;span style="color: #2b91af"&gt;_repository.GetAllClientQuote&gt;&gt;().Skip(clientQuoteIndex).First();
} &lt;span style="color: blue"&gt;#endregion &lt;/span&gt;}&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
&lt;p&gt;
This website currently lives in 3 different environments. My local (development) environment,
a Staging Environment also hosted by Softsys Hosting, and the Production Environment.
Since I’m limited to a certain number of databases with my shared hosting plan, I
didn’t want to go and create a Staging database for the website. I was trying to think
of some ways to “physically” handle this problem. 
&lt;p&gt;
&lt;p&gt;
First, I thought about using the Production database for Staging and just creating
“Staging” tables that mimicked production. So if I had a “ClientQuote” table in Production,
I could have a “Staging_ClientQuote” table for Staging. Doing that would be wrong
on so many levels. The most annoying being database migrations and creating different
entity classes for all my database tied entities. 
&lt;p&gt;
&lt;p&gt;
So instead, I stepped back and looked at what was really going on in my application
to generate these quotes. The only thing my HomeController needed in it’s RandomQuote
method was an instance of “ClientQuote”. What I really needed to do was provide an
alternative way to get this quote in an environment that doesn’t use a database. I
needed ANOTHER concrete class to be used in my staging environment. Enter StagingClientQuoteService. 
&lt;p&gt;
&lt;p&gt;
Snippet from &lt;strong&gt;StagingClientQuoteService.cs&lt;/strong&gt; 
&lt;p&gt;
&lt;/p&gt;
&lt;table border="1" cellspacing="0" cellpadding="2" width="700"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td valign="top" width="698"&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;StagingClientQuoteService &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;IClientQuoteService &lt;/span&gt;{ &lt;span style="color: blue"&gt;#region &lt;/span&gt;IClientQuoteService
Members &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ClientQuote &lt;/span&gt;GetRandomClientQuote()
{ &lt;span style="color: blue"&gt;return new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ClientQuote &lt;/span&gt;{
City = &lt;span style="color: #a31515"&gt;"Chicago"&lt;/span&gt;, Id = 1, Quote = &lt;span style="color: #a31515"&gt;"Jamie
Lynn Designs is really amazing! What else can I say?"&lt;/span&gt;, State = &lt;span style="color: #a31515"&gt;"IL"&lt;/span&gt;,
SubmittedBy = &lt;span style="color: #a31515"&gt;"Dave" &lt;/span&gt;}; } &lt;span style="color: blue"&gt;#endregion &lt;/span&gt;}&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
&lt;p&gt;
So here, I now have a class that just creates the same ClientQuote over and over each
time it is called and returns it. I could’ve gotten more complex and created some
more “database free” random quotes and returned those instead. 
&lt;p&gt;
&lt;p&gt;
Using the extension Unity.Configuration block, I’m able to easily swap between my
“real” ClientQuoteService and StagingClientQuoteService. Below I’m omitting some of
the other Unity Mappings to just show what I did for the IClientQuoteService interface. 
&lt;p&gt;
&lt;p&gt;
&lt;p&gt;
Snippet from PRODUCTION &lt;strong&gt;web.config&lt;/strong&gt; 
&lt;p&gt;
&lt;strong&gt;&lt;/strong&gt; 
&lt;/p&gt;
&lt;table border="1" cellspacing="0" cellpadding="2" width="700"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td valign="top" width="698"&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;unity&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;&lt;/span&gt;&gt; &lt;span style="color: #a31515"&gt;containers&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;&lt;/span&gt;&gt; &lt;span style="color: #a31515"&gt;container&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;&lt;/span&gt;&gt; &lt;span style="color: #a31515"&gt;types&lt;/span&gt;&lt;span style="color: blue"&gt;&gt; &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;&lt;/span&gt; &lt;span style="color: #a31515"&gt;type &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;JamieLynnDesigns.Core.Services.IClientQuoteService,JamieLynnDesigns.Core&lt;/span&gt;" &lt;span style="color: red"&gt;mapTo&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;JamieLynnDesigns.Core.Services.ClientQuoteService,JamieLynnDesigns.Core&lt;/span&gt;"&lt;span style="color: blue"&gt;&lt;/span&gt;&gt; &lt;span style="color: #a31515"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;&gt; &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;types&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;/span&gt;&gt; &lt;span style="color: #a31515"&gt;container&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;/span&gt;&gt; &lt;span style="color: #a31515"&gt;containers&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;/span&gt;&gt; &lt;span style="color: #a31515"&gt;unity&lt;/span&gt;&lt;span style="color: blue"&gt;&gt;&lt;/span&gt;
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
&lt;p&gt;
Snippet from STAGING &lt;strong&gt;web.config&lt;/strong&gt;
&lt;/p&gt;
&lt;table border="1" cellspacing="0" cellpadding="2" width="730"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td valign="top" width="728"&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;unity&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;&lt;/span&gt;&gt; &lt;span style="color: #a31515"&gt;containers&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;&lt;/span&gt;&gt; &lt;span style="color: #a31515"&gt;container&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;&lt;/span&gt;&gt; &lt;span style="color: #a31515"&gt;types&lt;/span&gt;&lt;span style="color: blue"&gt;&gt; &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;type &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;JamieLynnDesigns.Core.Services.IClientQuoteService,JamieLynnDesigns.Core&lt;/span&gt;" &lt;span style="color: red"&gt;mapTo&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;JamieLynnDesigns.Core.Services.Fake.StagingClientQuoteService,JamieLynnDesigns.Core&lt;/span&gt;"&lt;span style="color: blue"&gt;&lt;/span&gt;&gt; &lt;span style="color: #a31515"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;&gt; &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;types&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;/span&gt;&gt; &lt;span style="color: #a31515"&gt;container&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;/span&gt;&gt; &lt;span style="color: #a31515"&gt;containers&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;/span&gt;&gt; &lt;span style="color: #a31515"&gt;unity&lt;/span&gt;&lt;span style="color: blue"&gt;&gt;&lt;/span&gt;
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
And that’s it! There are many other scenarios where this kind of thing comes in useful.
The only thing I am watching out for is creating too many of my own “fake” classes,
so my projects don’t come to large with “non production” code. But as long as there’s
a good logical separation, such as a folder named “Staging Mocks” or something of
the sort in your project, you should be okay.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.davearlin.com/aggbug.ashx?id=0c2deede-9c40-423d-b630-a550f2c6b548" /&gt;</description>
      <comments>http://blog.davearlin.com/CommentView,guid,0c2deede-9c40-423d-b630-a550f2c6b548.aspx</comments>
      <category>ASP.Net MVC</category>
      <category>Inversion of Control (IoC)</category>
      <category>Unity Application Block</category>
    </item>
  </channel>
</rss>