Enum properties for EPiServer 7.5
Joel Abrahamsson wrote a nice blog post about how to define a property that uses an enum that gives the editor a selection of choices in the UI. This code was written for EPiServer 7 and there are some changes in EPiServer 7.5 that makes it possible to do some small improvements to this pattern. So here is a EPiServer 7.5 version for the same use case.
First, lets start with just copying the selection factory from the original blog post:
The difference with the original approach is that we want to use attributes that are using the IMetadataAware interface. This is an interface defined in System.Web.MVC that makes it possible to modify the metadata used for editing from an attribute and EPiServer 7.5 supports the usage of this. We will inherit from SelectOneAttribute, that handles definition of the editor to be used and just provide this with the correct selection factory instance for the given enum:
And the usage in your models would look like this:
Note 1: The need to define the backingtype attribute for enums is fixed in EPiServer.CMS 8.1.
Note 2: You could argue that you should be able to resolve the enum type from the type of the property in the OnMetadataCreated method. That would work in plain MVC but unfortunately this information is lost in the changes done to support properties that does not have a model (ie, non-typed models).
The result is a drop down with the potentially localized enum values, just as in the original blog post: