ASP.Net MVC Custom Model Binding explanation [closed]
Asked Answered
D

2

35

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.

Dolabriform answered 28/7, 2009 at 11:20 Comment(0)
D
31

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

Dolabriform answered 8/8, 2009 at 18:44 Comment(1)
odetocode.com/Blogs/scott/archive/2009/05/05/12801.aspx is a great link. Remember to add the custom model binder to the modelbinders binders collection in the application_start()Medan
S
4

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 :-)

Sweptback answered 28/7, 2009 at 14:37 Comment(1)
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().Dolabriform

© 2022 - 2024 — McMap. All rights reserved.