tags:

views:

505

answers:

2

Is anyone aware of where I can find a tutorial for building a custom model binder.

I have found several issues using the default model binder with EntitySets (not populating when creating new objects and creating duplicates on editing objects) and find that my UI doesn't match my model exactly (e.g. time fields formated "hh:mm am" do not match with a timespan). I therefore want to look at whether creating a custom model binder is a better solution. I have found a couple of examples: here and here. The first is I believe out of date and the second doesn't really explain what is going on.

My main problem is that I don't fully understand what the ControllerContext and ModelBindingContext Of the BindModel method actually are, i.e. how they relate to the controller and model.

I would be grateful if someone point me to a tutorial or could take a look at the dave hayden's post and show me how it might look with the current implementation of MVC.

+1  A: 

Here's a good explanation of ModelBinders that I believe will cover your questions: http://www.singingeels.com/Articles/Model_Binders_in_ASPNET_MVC.aspx

A quick Google search pulled up a few others... I will let you filter through them :-)

mc2thaH
Thanks, I had actually read that article and the follow up, but this and most of the other were written prior to refactoring in the beta realease. Instead of inheriting from DefaultModelBinder and overriding GetValue, the approach is now to implement IModelBinder and therefore provide BindModel AFAIK. I can probably work though Scott Hanselman's example if I can understand what the inputs and output are to BindModel().
Richbits
+1  A: 

I've actually now found the following two posts which a good summary of model binding:

http://odetocode.com/Blogs/scott/archive/2009/04/27/12788.aspx

http://odetocode.com/Blogs/scott/archive/2009/05/05/12801.aspx

Richbits