Drop downs are a part of any web application. And in ASP.NET MVC 3, drop down lists are preapred by using @Html.DropDownListFor() method.

I always keep on forgetting on how to

  1. Populate a dropdown list
  2. Read the selected value back into the controller,

So I have decided to write this as an article which would help me save time in future and this could also serve as a reference article to those who are new to DropDownList in MVC.

Lets start..

At the controller side, you need to have a List of SelectListItem type. I have passed this list to my ViewBag.DropDownData, here you could have passed it to your model instead, but I am trying to keep it as simple as possible. So you pass the list to a viewbag.

Controller code

var myList = new List<SelectListItem>
new SelectListItem {Text = "Yasser", Value = "YS"},
new SelectListItem {Text = "Vaibhav", Value = "VS"},
new SelectListItem {Text = "Ramanjit", Value = "RS"}

ViewBag.DropDownData = myList;

Now in the view using the @Html.DropDownListFor() method, the first argument should be a string/int (depending upon your value) declared in your model which will be later be used when data is posted back to the controller. The second parameter is the List we just prepared, in case if you are using a model then simply pass your model here instead.

Razor View

@Html.DropDownListFor(m => m.ValueSelected, ViewBag.DropDownData as List<SelectListItem>)

There that’s it. Your dropdown is ready to go :), here’s the ouput of my code:


Some other ways

ASP.NET MVC 3 Dropdownlist of Users