World is now on Opti ID! Learn more

Linus Ekström
May 6, 2014
  9923
(0 votes)

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:

using System;
using System.Collections.Generic;
using EPiServer.Framework.Localization;
using EPiServer.Shell.ObjectEditing;
 
namespace EPiServer.Samples
{
    public class EnumSelectionFactory<TEnum> : ISelectionFactory
    {
        public IEnumerable<ISelectItem> GetSelections(
            ExtendedMetadata metadata)
        {
            var values = Enum.GetValues(typeof(TEnum));
            foreach (var value in values)
            {
                yield return new SelectItem
                {
                    Text = GetValueName(value),
                    Value = value
                };
            }
        }
 
        private string GetValueName(object value)
        {
            var staticName = Enum.GetName(typeof(TEnum), value);
 
            string localizationPath = string.Format(
                "/property/enum/{0}/{1}",
                typeof(TEnum).Name.ToLowerInvariant(),
                staticName.ToLowerInvariant());
 
            string localizedName;
            if (LocalizationService.Current.TryGetString(
                localizationPath,
                out localizedName))
            {
                return localizedName;
            }
 
            return staticName;
        }
    }
}

 

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:

using System;
using System.Web.Mvc;
using EPiServer.Shell.ObjectEditing;
 
namespace EPiServer.Samples
{
    public class EnumAttribute : SelectOneAttribute, IMetadataAware
    {
        public EnumAttribute(Type enumType)
        {
            EnumType = enumType;
        }
 
        public Type EnumType { get; set; }
 
        public new void OnMetadataCreated(ModelMetadata metadata)
        {
            var enumType = metadata.ModelType;
 
            SelectionFactoryType = typeof(EnumSelectionFactory<>).MakeGenericType(EnumType);
 
            base.OnMetadataCreated(metadata);
        }
    }
}

 

And the usage in your models would look like this:

[BackingType(typeof(PropertyNumber))]
[EnumAttribute(typeof(Priority))]
public virtual Priority Priority { get; set; }

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:

1972_2306

May 06, 2014

Comments

Please login to comment.
Latest blogs
Make Global Assets Site- and Language-Aware at Indexing Time

I had a support case the other day with a question around search on global assets on a multisite. This is the result of that investigation. This co...

dada | Jun 26, 2025

The remote server returned an error: (400) Bad Request – when configuring Azure Storage for an older Optimizely CMS site

How to fix a strange issue that occurred when I moved editor-uploaded files for some old Optimizely CMS 11 solutions to Azure Storage.

Tomas Hensrud Gulla | Jun 26, 2025 |

Enable Opal AI for your Optimizely products

Learn how to enable Opal AI, and meet your infinite workforce.

Tomas Hensrud Gulla | Jun 25, 2025 |

Deploying to Optimizely Frontend Hosting: A Practical Guide

Optimizely Frontend Hosting is a cloud-based solution for deploying headless frontend applications - currently supporting only Next.js projects. It...

Szymon Uryga | Jun 25, 2025

World on Opti ID

We're excited to announce that world.optimizely.com is now integrated with Opti ID! What does this mean for you? New Users:  You can now log in wit...

Patrick Lam | Jun 22, 2025

Avoid Scandinavian Letters in File Names in Optimizely CMS

Discover how Scandinavian letters in file names can break media in Optimizely CMS—and learn a simple code fix to automatically sanitize uploads for...

Henning Sjørbotten | Jun 19, 2025 |