Try our conversational search powered by Generative AI!

Unknown
Jan 26, 2015
  4577
(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
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

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog