使用枚举创建下拉列表

时间:2012-01-20 06:42:34

标签: asp.net-mvc-3 enums drop-down-menu html-helper editorfor

  

可能重复:
  MVC3 Razor DropDownListFor Enums

我是ASP.NET MVC3的新手(真的很新)。我想为四年级学生创建一个下拉列表框。我有:“第一”,“第二”,“第三”和“第四”。现在,在模型中,我创建了一个名为YLevels的枚举,如下所示:

  public enum YLevels
    {
      First =1,
      Second,
      Third ,
      Fourth
    }

总的来说,我的模型类StudentMT包含:

 public StudentMT()
    {
        Remarks = string.Empty;
    }

    public int Id { get; set; }

    [Required(ErrorMessage = "First Name is required.")]
    [StringLength(30, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 3)]
    [Display(Name= "First Name")]
    public string FName { get; set; }

    [Required(ErrorMessage = "Last Name is required.")]
    [StringLength(30, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 3)]
    [Display(Name = "Last Name")]
    public string LName { get; set; }

    public string Gender { get; set; }

    [Display(Name="Year Level")]
    public int YLevel { get; set; }

    public string Remarks { get; set; }

    public enum YLevels
    {
        First =1,
        Second,
        Third ,
        Fourth
    }
  }

然后在我看来,我想使用EditorFor():

    <div class="editor-label">
        @Html.LabelFor(model => model.YLevel)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.YLevel)
        @Html.ValidationMessageFor(model => model.YLevel)<br/> 
    </div>

如何定义使用此枚举的属性?如何使用EditorFor()显示下拉列表?请提出我可以做的建议。

提前谢谢!

1 个答案:

答案 0 :(得分:0)

你可以尝试一下:

[Display(Name="Year Level")]
public YLevels YLevel { get; set; }