How to create a static dropdown in MVC?


Option 1:

@Html.DropDownList("Gender", new List<SelectListItem>{

new SelectListItem{ Text="Male", Value="Male"},

new SelectListItem{ Text="Female", Value="Female"}

}, "Select Gender")



Option 2: 

Controller

public ActionResult DropDownListExampleView()

{

   var list = new List<SelectListItem>

   {

       new SelectListItem{ Text="Male", Value = "Male" },

       new SelectListItem{ Text="Female", Value = "Female" },

       new SelectListItem{ Text="Select Gender", Value = "0", Selected = true },

   }); 

   ViewData["ddList"] = list;

   return View();

}


In View


@Html.DropDownList("Gender", ViewData["ddList"] as List<SelectListItem>)

Post a Comment

1 Comments