如何为枚举下拉列表创建强类型标签

时间:2017-06-29 20:53:12

标签: c# asp.net-mvc razor enums asp.net-mvc-5

如何为枚举下拉列表创建强类型标签。所以我有一个enum下拉列表,我希望有一个标签,但我希望它强烈地键入enum属性。这不可能吗?

型号:

  public class CourseEnumsModel
    {
        public CourseEnums OptionSelected { get; set; }

        public string Error { get; set; }
    }

ENUMS:

   public enum CourseEnums
    {
        Java = 0,
        CSharp = 1,
        RubyOnRails = 2
    }

查看:

@model WebApplication3.Models.CourseEnumsModel
@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>
<form method="post">
 // ONE STRONGLY TYPED LABEL HERE FOR THE DROPDOWN
   @Html.EnumDropDownListFor(model => model.OptionSelected)
   <input type="submit" value="Send" />
</form>

基本字符串属性

我们有显示标签
<dt>
    // I want something like this for the enum dropdown
    @Html.DisplayNameFor(model => model.AccountNumber)
</dt>

<dd>
    @Html.DisplayFor(model => model.AccountNumber)
</dd>

1 个答案:

答案 0 :(得分:1)

我认为这应该有用

@Html.DisplayNameFor(model => model.OptionSelected )

public class CourseEnumsModel
    {
        DisplayName("<YOUR NAME>")
        public CourseEnums OptionSelected { get; set; }

        public string Error { get; set; }
    }