What do I need to do if I want all typeof(MyEnum)'s to be handled with MyEnumModelBinder?

Posted by Byron Sommardahl on Stack Overflow See other posts from Stack Overflow or by Byron Sommardahl
Published on 2010-05-05T16:40:16Z Indexed on 2010/05/05 17:28 UTC
Read the original article Hit count: 171

Filed under:
|

I have an Enum that appears in several of my models. For the most part, the DefaultModelBinder handles binding to my models beautifully. That it, until it gets to my Enum... it always returns the first member in the Enum no matter what is handed it by the POST.

My googling leads me to believe I need to have a model binder that knows how to handle Enums. I found an excellent article on a possible custom modelBinder for Enums: http://eliasbland.wordpress.com/2009/08/08/enumeration-model-binder-for-asp-net-mvc/. I've since implemented that modelBinder and registered it in my global.asax:

ModelBinders.Binders[typeof (MyEnum)] = new EnumBinder<MyEnum>(MyEnum.MyDefault);

For some reason, the EnumBinder<> isn't being called when the model I'm binding to has MyEnum. I have a breakpoint in the .BindModel() method and it never break. Also, my model hasn't changed after modelBinding.

Have I done everything? What am I missing here?

© Stack Overflow or respective owner

Related posts about asp.net-mvc-2

Related posts about modelbinders