This example shows you how to create a Enum Dropdown in ASP.Net MVC4.

We need to show Sex as Male or Female in Employee Screen

Step1:

Create a Class which contains the Enumlist under a Folder Enums.

 

namespace MVCApp.Enums

{

    public class MyEnums

    {

 

        public enum Gender

        {

            Male=0,

            Female=1

        }

    }

}

Step2.

Create a Employee Model as below.

using MVCApp.Enums;

 

 

namespace MVCApp.Models

{

    public class Employee

    {

               

        public int ID { get; set; }

        public string Name { get; set; }

 

        public MyEnums.Gender Sex { get; set; }

 

        public string Post { get; set; }

 

        public decimal Salary { get; set; }

 

        public string City { get; set; }

    }

}

 

Step 3.

In Create.CSHTML define as below

<divclass="form-group">

            @Html.LabelFor(model => model.Sex, new { @class = "control-label col-md-2" })

            <divclass="col-md-10">

               @Html.DropDownListFor(model => model.Sex,newSelectList(Enum.GetValues(typeof(MVCApp.Enums.MyEnums.Gender))))

                @Html.ValidationMessageFor(model => model.Sex)

            </div>

        </div>