Aleksei Chepovoi
Aleksei Chepovoi

Reputation: 3955

The difference between IModelBinder and IModelBinderProvider asp.net mvc 3

The question is simple: when should I implement IModelBinder and when IModelBinderProvider?

Thanks in advance!

Upvotes: 4

Views: 1372

Answers (2)

Wolf
Wolf

Reputation: 6499

When you use IModelBinderProvider you still need use IModelBinder for binder class

  We use IModelBinderProvider to help us don't need to register manual like
    ModelBinders.Binders.Add(typeof(HomePageModels1), new HomeCustomBinder1());
    ModelBinders.Binders.Add(typeof(HomePageModels2), new HomeCustomBinder2());

...

We just need add 1 line

ModelBinderProviders.BinderProviders.Add(new YourModelBinderProvider());

An when controller has parameter. it will auto call method

public IModelBinder GetBinder(Type modelType)

For you customize your model

Upvotes: 0

Jamie Ide
Jamie Ide

Reputation: 49261

IModelBinderProvider is a factory that supplies IModelBinder implementations. For simple cases where a type always uses the same model binder you just need to use IModelBinder. You would implement IModelBinderProvider for more complex cases where you need to dynamically determine which model binder to use.

Jimmy Bogard and Brad Wilson explain it better than I can.

Upvotes: 2

Related Questions