How to Bind Enum Types to the DropDownList?

后端 未结 6 1178
终归单人心
终归单人心 2020-12-24 11:29

If I have the following enum

public enum EmployeeType
{
    Manager = 1,
    TeamLeader,
    Senior,
    Junior
}

and I have DropDownList

6条回答
  •  既然无缘
    2020-12-24 12:05

    I wrote a helper function to give me a dictionary that I can bind:

    public static Dictionary GetDictionaryFromEnum()
    {
    
        string[] names = Enum.GetNames(typeof(T));
    
        Array keysTemp = Enum.GetValues(typeof(T));
        dynamic keys = keysTemp.Cast();
    
        dynamic dictionary = keys.Zip(names, (k, v) => new {
            Key = k,
            Value = v
        }).ToDictionary(x => x.Key, x => x.Value);
    
        return dictionary;
    }
    

提交回复
热议问题