2015-10-14 25 views
1

我正在尋找將僱員類型設置爲經理或員工,我創建了一個枚舉屬性,但我無法獲得經理和員工值填充我的創建視圖。需要將枚舉值作爲下拉選項在我的視圖

我的模型

 public string LastName { get; set; } 
    public enum EmployeeType {Employee, Manager } 
    public virtual Store Store { get; set; } 
    public int AssignedStore { get; set; } 

我查看

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

您使用的是什麼版本的MVC? –

+0

VS 2013上的MVC 5 – buckeyebmr

+3

使用'@ Html.EnumDropDownListFor'。它僅在MVC 5中添加,但由於您使用的是MVC 5,因此您可以輕鬆前往。 –

回答

1

您不能引用一個枚舉,如果它是你的模型的屬性。你的模型應該是這樣的:

public class Foo 
{ 
    public string LastName { get; set; } 
    public EmployeeType EmployeeType { get; set; } 
    public virtual Store Store { get; set; } 
    public int AssignedStore { get; set; } 
} 

public enum EmployeeType 
{ 
    Employee, 
    Manager 
} 

注意,枚舉類之外定義,然後類與您的枚舉作爲類型的屬性。現在,您擁有一個實際上可以保存價值的屬性,並且可以在您的視圖中使用該屬性:

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

的課程。謝謝,那很好用。 – buckeyebmr