【问题标题】:Show Enum values according to Display attribute in DropDownList [duplicate]根据 DropDownList 中的 Display 属性显示枚举值 [重复]
【发布时间】:2023-03-23 03:51:01
【问题描述】:

我有这门课State:

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

考虑到Display 属性是用户友好的,如何使用Display 属性在DropDownList 中显示enum 值? 我一直在尝试,但我总是得到原始值(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

【问题讨论】:

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


    【解决方案1】:
    <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>
    

    【讨论】:

    • 它在我的项目中不起作用..我得到相同的结果..NotSubmitted,RepeatRequest...
    猜你喜欢
    • 1970-01-01
    • 2017-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 2018-04-14
    • 2016-09-04
    相关资源
    最近更新 更多