November Happy Hour will be moved to Thursday December 5th.

Make editor set text of c# enum

Vote:
 

Hi!

I have a C# enum:

public enum Priority
{
Low= 0,
Normal= 1,
High = 2
}

I would like an editor to be able to associate a text to the enum values like Low => "This is the lowest priority"

Is there a Epi Server property for this?

#162418
Oct 14, 2016 10:32
Vote:
 
#162421
Oct 14, 2016 11:36
Vote:
 

But doesn't that solution only allow the editor to choose which value that the property of the enum holds?

I.e the editor can choose if the property should be Low, Normal, High. It doesn't provide the editor with a way to set the text for Low, Normal, High?

#162422
Oct 14, 2016 12:25
Vote:
 

Not built in but you might use two properties that depends on each others, like they do in this example (but you will have an ordinary enum dropdown and then a simple textbox property and then you build it togheter in your viewmodel)

http://world.episerver.com/blogs/Duong-Nguyen/Dates/2014/1/Country-Region-drop-down-lists-in-All-properties-mode/

#162423
Oct 14, 2016 12:57
Vote:
 

You could have a page type with 2 properties:

1. Dropdown with enum values

2. Text field with friendly name

Then add one page each per option (and language). Then build logic to get friendly name by searching these pages by value...

Add a container page to make it easy to create new options...(restricted to the pagetype above)

A bit old style but works :)

#162427
Oct 14, 2016 14:00
Vote:
 

Hi,

Thanks for the help. Just wanted to tell you what I ended up doing:

I used this library: https://github.com/joseftw/JOS.PropertyKeyValueList and modified the KeyValueAttribute like

[AttributeUsage(AttributeTargets.Property)]
    public class EnumNameValueAttribute : Attribute
    {
        public List<string> ReadOnlyKeys;

        public EnumNameValueAttribute()
        {
            this.ReadOnlyKeys = new List<string>();
        }

        public EnumNameValueAttribute(Type enumType)
        {
            this.ReadOnlyKeys = Enum.GetNames(enumType).ToList();
        }
    }

This enables the editor to set texts on the Enum.

#162800
Oct 17, 2016 8:50
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.