Don't miss out Virtual Happy Hour today (April 26).

Try our conversational search powered by Generative AI!

Unknown
Jan 26, 2015
  4580
(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
Azure AI Language – Extractive Summarisation in Optimizely CMS

In this article, I demonstrate how extractive summarisation, provided by the Azure AI Language platform, can be leveraged to produce a set of summa...

Anil Patel | Apr 26, 2024 | Syndicated blog

Optimizely Unit Testing Using CmsContentScaffolding Package

Introduction Unit tests shouldn't be created just for business logic, but also for the content and rules defined for content creation (available...

MilosR | Apr 26, 2024

Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog