0

I have a list of objects of a class like:

public class Template
{
    public TemplateTypeEnum TemplateType { get; set; }
    public int Id { get; set; }
    public int Name { get; set; }
    /// etc...
}

And enum:

public enum TemplateTypeEnum 
{
    [StringValue("First item")]
    FirstItem = 1,
    [StringValue("Second item")]
    SecondItem = 2,
    // .....
 }

and I have a list of Template objects:

IEnumerable<Template> templateList = GetAllTemplates();

and I would like to populate a select list only from Enum values, so that data value field is int representation of the enum value, and data text file is the string value of the enum, so something like:

 Model.TemplateList = new SelectList(templateList, "(int)TemplateType", "TemplateType.ToString()");
Mefhisto1
  • 2,188
  • 7
  • 34
  • 73

3 Answers3

0
var templates = new List<Template>();
var values = Enum.GetValues(typeof(TemplateTypeEnum));
foreach (var value in values)
{
    templates.Add(new Template() { Name = Enum.GetName(typeof(TemplateTypeEnum), value), TemplateType = (TemplateTypeEnum)value, Id = (int)value });
}

Later edit:

For retrieving attribute name, it may help the answers given on a similar question, here: Getting attributes of Enum's value

Lavinia N.
  • 246
  • 1
  • 4
  • 13
0

"data text file is the string value of the enum"

This is ambiguous so I'm assuming you want the attribute StringValue in the list, rather than the actual representation of the enum value as a string.

Given an extension method like this:

public static class AttributeExtensions {
    public static TResult GetEnumAttributeValue<TEnum, TAttribute, TResult>(this TEnum value,
       Func<TAttribute, TResult> valueFunc)
        where TAttribute : Attribute
        where TEnum : struct, IComparable, IFormattable {

        var field = typeof(TEnum).GetField(value.ToString());

        if (field == null) {
            return default(TResult);
        }

        var attribute = field.GetCustomAttribute<TAttribute>();

        if (attribute == null) {
            return default(TResult);
        }

        return valueFunc.Invoke(attribute);
    }
}

You can retrieve the attribute values for the list: you have to cheat a bit in order to use the SelectList constructor by creating an interim list with properties you can pass as strings: this example assumes your StringValue attribute has a property called Value

var interimList = templateList.Select(t => new 
     { 
        Id = (int)t.TemplateType, 
        Value = t.TemplateType.GetEnumAttributeValue((StringValue) s) => s.Value);
     });

Model.TemplateList = new SelectList(interimList, "Id", "Value");

(It also assumes that you can use an anonymous type to populate a SelectList..)

stuartd
  • 70,509
  • 14
  • 132
  • 163
-1

You can use the built-in EnumDropDownListFor method.

Change the attribute on your enum to use DisplayAttribute:

public enum TemplateTypeEnum
{
    [Display(Name = "First item")]
    FirstItem = 1,
    [Display(Name = "Second item")]
    SecondItem = 2
}

View model:

public class Template
{
    public TemplateTypeEnum? TemplateType { get; set; }
}

Razor:

@Html.EnumDropDownListFor(x => x.TemplateType)
kuujinbo
  • 9,272
  • 3
  • 44
  • 57