根据DropDownList中的Display属性显示枚举值

时间:2017-07-31 14:24:27

标签: c# html asp.net-mvc drop-down-menu enums

我有这个课程State

public enum State
    {
        Ok,
        [Display(Name = "Not Submitted")]     
        NotSubmited,
        [Display(Name = "Repeat Request")]
        RepeatRequest,
        Sent,
        Error,
        Response
    }

考虑到DropDownList属性是否用户友好,如何使用enum属性显示Display Display个值? 我一直在尝试,但我总是得到原始值(NotSubmitted, RepeatRequest没有空格..)

这是我DropDownList的代码:

   <select name="Response[@index].State" class="form-control">
        <option value="">Select State</option>
        @foreach (var state in Enum.GetValues(typeof(State)))     
            {
                <option>@state</option>
            }
    </select>

var ValuesArray =Enum.GetValues(typeof(Status));
var NamesArray = Enum.GetNames(typeof(Status));

但仍然总是得到不友好的价值......(例如NotSubmitted代替Not Submitted

1 个答案:

答案 0 :(得分:0)

<select name="Response[@index].State" class="form-control">
    <option value="">Select State</option>
    @foreach (var state in Enum.GetValues(typeof(State)))     
        {
            <option>Enum.GetName(typeof(State), state )</option>
        }
</select>