EnumDropDownList MVC 5

时间:2014-06-26 08:40:53

标签: asp.net scaffolding

我有课

public partial class Team
{
    public int Id { get; set; }
    public Shifts Shift { get; set; }
}

public enum Shifts : int
{
    First = 1,
    Second = 2
}

和MVC视图代码

    <div class="form-group">
        @Html.LabelFor(model => model.Shift, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EnumDropDownListFor(model => model.Shift, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Shift, "", new { @class = "text-danger" })
        </div>
    </div>

和控制器发布代码......

    public ActionResult Create([Bind(Include = "Shift")] Team team)
    {
        if (ModelState.IsValid)
        {
            db.TeamSet.Add(team);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(team);
    }

在我的数据库中,Shift是int类型,我试图从Enum获取值,但是从表单中发布我得到文本(&#34; First&#34;或&#34; Second&#34; )。我试图找到一些文档EnumDropDownListFor但没有成功,所以如果有人有任何想法,我会感恩..

1 个答案:

答案 0 :(得分:0)

public enum Shifts : int
{

[Display(Name = "First")]
First = 1,

[Display(Name = "Second")]
Second = 2
}