我是 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() 显示下拉列表?请建议我能做什么。
先感谢您!