ModelBinding

TwitterFacebook
Get flash to fully experience Pearltrees

Model Binding Decimal Values

http://haacked.com/archive/2011/03/19/fixing-binding-to-decimals.aspx I’m in the beautiful country of Brazil right now (I’ll hopefully blog more about that later) proctoring for the hands-on labs that’s part of the Web Camps agenda. However, the folks here are keeping me on my toes asking me to give impromptu and deeply advanced demos. It almost feels like a form of performance art as I create brand new demos on the fly. During this time, several people reported issues binding to a decimal value that prompted me to write a new demo and this blog post. Let’s look at the scenario. Suppose you have the following class ( Jogador is a soccer player in Portugese):
Update: Apparently this works in MVC 1 and 2 – Thanks, Mike! One of the things you’ll find yourself doing quite often is loading an object from a database or other source given an id from a url. Something along the lines of http://localhost/users/details/john and then loading the User model with the username of “john”. public ActionResult Details ( string username ) { var user = db . Users . http://buildstarted.com/2010/09/12/custom-model-binders-in-mvc-3-with-imodelbinder/

Custom Model Binders in MVC 3 with IModelBinder » BuildStarted.com

While digging deeper into MVC Views, I stumbled on this method – ViewData.Eval(). Found it interesting and researched and played with some code around this method. This method gives the user a way to search through the ViewData’s object graph. So in order to get the data, you can either do 1: <%= ViewData[ "Message" ] %> or http://weblogs.asp.net/nmarun/archive/2009/11/26/asp-net-mvc-viewdata-eval-method.aspx

MVC ViewData.Eval() method - IBloggable - implemented

Download the sample project to play with the code as you read this blog post. Using the DefaultModelBinder in ASP.NET MVC, you can bind submitted form values to arguments of an action method. But what if that argument is a collection? Can you bind a posted form to an ICollection<T> ? Sure thing! http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

Model Binding To A List

ASP.NET MVC Model Binders

http://www.dalsoft.co.uk/blog/index.php/2010/05/21/mvc-model-binders/ In the last section of my MVC Templates post , I discussed how to use the Html.EditorFor() helper with complex types . Whilst this showed how you can use Html.EditorFor() and templates to produce a user interface for complex types, it didn’t show you how to bind the result when the user has posted to a controllers action. In this post I will extend the employee example, so that it displays the offices the the user selected. Download the code for this example

A better Model Binder

One of the more interesting extension points in ASP.NET MVC are the Model Binders. Model Binders are tasked with transforming the HTTP Form and Querystring information and coercing real .NET types out of them. A normal POST is merely a set of string key-value pairs, which isn’t that fun to work with. Back in the ASP 3.0 days, where I cut my teeth, we did a lot of “Request.Form(“CustFirstName”)” action, and just dealing with the mapping from HTTP to strong types manually. That wasn’t very fun. http://lostechies.com/jimmybogard/2009/03/18/a-better-model-binder/
http://odetocode.com/blogs/scott/archive/2009/04/27/6-tips-for-asp-net-mvc-model-binding.aspx

6 Tips for ASP.NET MVC Model Binding

Model binding in the ASP.NET MVC framework is simple. Your action methods need data, and the incoming HTTP request carries the data you need. The catch is that the data is embedded into POST-ed form values, and possibly the URL itself. Enter the DefaultModelBinder, which can magically convert form values and route data into objects. Model binders allow your controller code to remain cleanly separated from the dirtiness of interrogating the request and its associated environment.