<?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' - IModelBinder</title>
    <link>http://blog.davearlin.com/</link>
    <description>learn forever</description>
    <language>en-us</language>
    <copyright>Dave Arlin</copyright>
    <lastBuildDate>Fri, 30 Apr 2010 03:15:59 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=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>
  </channel>
</rss>