Home Creating a select list from an list of objects that have one enum property
Reply: 3

Creating a select list from an list of objects that have one enum property

Mefhisto1
1#
Mefhisto1 Published in 2018-01-12 14:36:15Z

This question already has an answer here:

  • How do you create a dropdownlist from an enum in ASP.NET MVC? 35 answers

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()");
Lavinia N.
2#
Lavinia N. Reply to 2018-01-12 14:53:46Z
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

stuartd
3#
stuartd Reply to 2018-01-12 15:08:58Z

"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..)

kuujinbo
4#
kuujinbo Reply to 2018-01-12 15:30:19Z

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)
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.314756 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO