Unknown
Jan 26, 2015
  4626
(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
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024