buckeyebmr
buckeyebmr

Reputation: 13

Need to get enum values as a drop down option in my View

I am looking to set an employeetype as either Manager or Employee, I created a enum property but am unable to get the Manager and Employee values to populate in my create view.

My Model

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

My View

        <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>

Upvotes: 1

Views: 73

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239270

You can't reference an enum as if it was a property on your model. Your model should be something like:

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
}

Notice that the enum is defined outside the class, and then the class has a property with your enum as the type. Now, you have a property that can actually hold a value, and this can be used in your view:

@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>

Upvotes: 1

Related Questions