Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more

Unknown
Jan 26, 2015
  4862
(1 votes)

CheckboxList using Enum

I recently implemented a checkboxlist using Enum
References:
  • http://joelabrahamsson.com/enum-properties-with-episerver/
  • http://world.episerver.com/blogs/Linus-Ekstrom/Dates/2013/12/SingleMultiple-selection-in-EPiServer-75/

My Code

public class EnumMultipleSelectionFactory<TEnum> : ISelectionFactory
{
    public IEnumerable<ISelectItem> GetSelections(
            ExtendedMetadata metadata)
    {
        var values = Enum.GetValues(typeof(TEnum));
        List<ISelectItem> items = new List<ISelectItem>();
        foreach (var value in values)
        {
            items.Add(new SelectItem { Text = GetValueName(value), Value = value.ToString() });
        }
                return items;
    }

    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;
    }
}



public class EventItemPage : SitePageData
{
    public enum EventCategoryValues
    {
        Arts,
        Business,
        Community,
        Festivals,
        Kids,
        Recreation,
        Sports
    }

    [Display(
        GroupName = SystemTabNames.Content,
        Order = 1030)]
    [SelectMany(SelectionFactoryType = typeof(EnumMultipleSelectionFactory<EventCategoryValues>))]
    public virtual string EventCategories { get; set; }

}


Jan 26, 2015

Comments

Matthew Hutton
Matthew Hutton Aug 25, 2015 10:55 AM

I've tried implementing this myself, and I've got it to write data to the database, but not to read data back out of the database.

See this post on StackOverflow for more details of what I did, but do you have any other ideas?

Please login to comment.
Latest blogs
Successful Digitalization for SMEs: How Optimizely One can Revolutionize Your Business Processes

"Achieve digital excellence with Optimizely One: Boost efficiency, delight customers, secure growth." In today's digital world, it's crucial for...

Frank Hohmeyer | Apr 11, 2025

Personalized Optimizely CMS Website Search Experiences Azure AI Search & Personalizer

In the last blog, we discussed Integrating the Optimizely CMS website with Azure AI search. Now let’s take a bit more advanced topic to serve...

Naveed Ul-Haq | Apr 10, 2025 |

Integrating Optimizely CMS with Azure AI Search – A Game-Changer for Site Search

Want to elevate your Optimizely PaaS CMS site’s search capabilities? Azure AI Search could be just the tool you need! In this blog, I’ll discuss......

Naveed Ul-Haq | Apr 9, 2025 |

Opensource release: New Package Explorer for Optimizely CMS

The import/export ".episerverdata" packages have been around as far as I can remember - and even though they might seem a bit outdated, it's still...

Allan Thraen | Apr 9, 2025 |