This project is read-only.

DefaultModelBinder - IModelBinderProvider ?

Jan 27, 2011 at 2:05 PM

Is there a possible issue when i change the DefaultModelBinder myself? Now or in the future?

I'm thinking about implimenting a custom modelBinder, as descripted in

Orchard seems to do something with ModelBinders, it has a ModelBinderProvider and ModelDescriptors etc.
And there isn't any documentation about it :)

Might it be enough to create a ModelBinder class in a module that impliments the interface IModelBinderProvider?
But will it replace the DefaultModelBinder then?

If not.. where should i replace the DefaultModelBinder, in a class implimenting the IOrchardShellEvent under Activated?

Jan 28, 2013 at 5:38 PM

Would like to revive this topic.

I implemented a default model binder that would catch HttpRequestValidationException to prevent the request being killed and instead add an error message to ModelState. I set this as the default binder by creating an Autofac module and setting System.Web.Mvc.ModelBinders.Binders.DefaultBinder, but I've found that sometimes my validation logic (I've also plugged in FluentValidation.NET) was being called multiple times from the model binder.

I'm wondering if it's related to how Orchard set's model binders so have now overridden the ModelBinderPublisher to set a default binder there. Not sure if it's working as my problem was intermittent in the first place.


Jan 31, 2013 at 10:12 AM
Nope that didn't work. Not sure the model binder is my issue here though...
Feb 10, 2013 at 9:56 PM
I don't know what's wrong in your case, but having tried to use custom binders many times, I can provide some advice. I don't even attempt to use them anymore, as I find them really tricky to set-up, and to restrict in scope. I usually try to model my forms so that they bind naturally using the default binders. They are amazingly powerful and I've never found a case that couldn't be solved using them. The form modeling can be a little constraining (in particular for collections and dictionaries), but it's basically headache-free once you know what they can and cannot do. Using edit view models also helps in some cases as intermediary data structures on updates.