views:

342

answers:

1

Is there an attribute that can turn off model binding for complex action parameters?

My scenario is this: I'm using Linq to SQL but make the generated classes implement interfaces. For example, IUser. Some of my controllers actions accept IUser parameters and some of my views bind IUser models, but I don't want the default model binder to construct it. I use Action Filters for that. The idea behind this is that if an action requires user details, the action filter gets it from the database before the action is invoked.

To get around the error that occurs when the DefaultModelBuilder gets fired up, I've made a null model binder that just returns null from CreateModel.

ModelBinderDictionary binders = System.Web.Mvc.ModelBinders.Binders;
binders[typeof(IUser)] = new NullModelBinder();
A: 

Would it not be simpler to follow the architecture and create an IUser binder that does what your action filter is doing? Currently you are perverting the action filter to do something it is not entirely intended to do and in the process you have also ignored the extension point designed specifically for doing what you are trying to achieve ( create an instance of a parameter in a specific manner ).

Is there good reason you are creating the IUser in the action filter rather than with a custom binder?

Neal