ASP.NET MVC Model Binding

| by Roopesh Shenoy Follow 0 Followers on Feb 13, 2012. Estimated reading time: 1 minute |

Model Binding is a feature that simplifies controller actions by using the request data to create strongly typed objects. Jess Chadwick takes a deep dive into this feature in an MSDN article and explores complex scenarios, as well as creating custom model binders when the default model binder is not enough. 

What is ASP.NET MVC Model Binding? It enables code like this - 

public ActionResult Create()

var product = new Product() {

AvailabilityDate = DateTime.Parse(Request["availabilityDate"]),
CategoryId = Int32.Parse(Request["categoryId"]),
Description = Request["description"],
Kind = (ProductKind)Enum.Parse(typeof(ProductKind),
Name = Request["name"],
UnitPrice = Decimal.Parse(Request["unitPrice"]),
UnitsInStock = Int32.Parse(Request["unitsInStock"])



to be replaced with this - 

public ActionResult Create(Product product)
// ...

ASP.NET automatically maps the query string parameter names with the property names in the strongly typed object. This also supports JSON post values. Model binding works even in advanced cases such as collections and nested objects, although with collections you have to be careful about the syntax (which involves using indexers to represent items in the collection). 

You still might want to extend the model binding frameworks with your own custom model binder in some cases. A common example is binding to interfaces and abstract classes, which needs strong coupling with the underlying business model (since the binder need to choose a particular implementation at runtime based on the request data). Jess's article shows how this can be achieved by just inheriting the DefaultBindingProvider and overriding behavior that’s necessary. For an example on how to unit test your custom model binders, refer Scott Hanselmann's article Splitting DateTime - Unit Testing ASP.NET MVC Model Binders

Rate this Article

Adoption Stage

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

News value? by M Vleth

No offense, but this is common in many mvc frameworks. Why this sudden attention?

Re: News value? by Roopesh Shenoy

Yes this feature isn't new, but the article by Jess is. Besides we haven't covered it before, so saw this as an opportunity to introduce it.

I am sure a lot of developers use this without giving a second thought about it, don't know if most of them know about whats' going on underneath (and how to customize the behavior if needed).

Re: News value? by David Carrillo

Hey sometimes you can learn one more thing even if you know the topic or you have used that technology before, so just try the article and maybe you can learn something new or remember something that you forgot

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

3 Discuss