Localized enums with Entity Framework Code-First 4.1 in WPF MVVM and ASP.NET MVC 3. Part 3)

By Mirek on (tags: ASP.NET MVC, Code First, Entity Framework, enums, lozalization, mvvm, WPF, categories: code)

Part 3. Displaying and binding localized names of enum values
in ASP.NET MVC 3 application

To display localized enums in ASP.NET MVC 3 application we use custom description attribute described in Part 2) and extension method to get the description for each enum value. The type converter is not useful here so we need another approach to achieve the goal.

Let’s generate the editing View for our Client object, where we want to have an text box for name and drop down list with preselected client’s Status and all possible values of Status type.

You can see hare an extension method ToSelectList() which looks following

It returns the list of SelectListItem with all values of enum type localized to current culture. Parameter selectMe indicates if current enum value has to be selected on the list.
Using this list in drop down generates html similar to following


Generally that could be enough to use localized enums in ASP.NET MVC 3. Values are properly localized, displayed and submitting the form generates properly filled Client model. Unfortunatelly the version of MVC 3 framework I had used had a bug which caused that html representation of enum value was not properly recognized and translated to enum type and submiting form produces error like on the following screen.

Underlying value Busy has not been recognized as enum type value.

As a solution for that I have used a custom model binder found on ASP.NET forums here, which after small modification works for my case. Just add following class

and to use this binder add following line in Global.asax file of MVC 3 application

These treatments are no longer needed in latest version of ASP.NET-MVC-3-Tools-Update-RTM framework, but I could not find out how to update my mvc 3 project to make the binding enums working. I also tried using an ASP.NET MVC 3 Application Updater available here, but it did not fix the problem, so the only solution was to use a custom model binder for binding enum values.