How to get the values of an enum into a SelectList

前端 未结 12 947
醉梦人生
醉梦人生 2020-12-13 11:58

Imagine I have an enumeration such as this (just as an example):

public enum Direction{
    Horizontal = 0,
    Vertical = 1,
    Diagonal = 2
}
12条回答
  •  天涯浪人
    2020-12-13 12:34

    It's been awhile since I've had to do this, but I think this should work.

    var directions = from Direction d in Enum.GetValues(typeof(Direction))
               select new { ID = (int)d, Name = d.ToString() };
    return new SelectList(directions , "ID", "Name", someSelectedValue);
    

提交回复
热议问题