How To Bind DropDownList In ASP.NET MVC

Introduction

This article describes how you can bind your MVC DropDownList in different ways.

Method 1
 
DropDownList in Controller, using ViewBag.

Step 1
 
Create Controller

Right click on Controller folder in the created MVC Application and give the class name. I have given the class name Home and clicked OK.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. namespace DeleteDrop.Controllers {  
  7.     public class HomeController: Controller {  
  8.         public ActionResult Index() {  
  9.             List < SelectListItem > items = new List < SelectListItem > ();  
  10.             items.Add(new SelectListItem {  
  11.                 Text = "add", Value = "Add"  
  12.             });  
  13.             items.Add(new SelectListItem {  
  14.                 Text = "sub", Value = "Sub"  
  15.             });  
  16.             items.Add(new SelectListItem {  
  17.                 Text = "mul", Value = "Mul"  
  18.             });  
  19.             items.Add(new SelectListItem {  
  20.                 Text = "Div", Value = "Div"  
  21.             });  
  22.             ViewBag.store = items;  
  23.             return View();  
  24.         }  
  25.     }  
  26. }  
Step 2
 
Add View

Open a HomeController class -> right click inside Index method -> click Add View.
  1. @ {  
  2.     Layout = null;  
  3. } <  
  4. !DOCTYPE html >  
  5.     <  
  6.     html >  
  7.     <  
  8.     head >  
  9.     <  
  10.     meta name = "viewport"  
  11. content = "width=device-width" / >  
  12.     <  
  13.     title > Index < /title> <  
  14.     /head> <  
  15.     body >  
  16.     <  
  17.     div >  
  18.     @Html.DropDownList("arithmatic", (IEnumerable < SelectListItem > ) ViewBag.store, "please select") <  
  19.     /div> <  
  20.     /body> <  
  21.     /html>  
Output

DropDownList

Method 2
 
DropDownList in View.

Step 1
 
Create Controller

Right click on the Controller folder in the created MVC Application and give the class name. I have given the class name Home and click OK.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. namespace DeleteDrop.Controllers {  
  7.     public class HomeController: Controller {  
  8.         public ActionResult Index() {  
  9.             return View();  
  10.         }  
  11.     }  
  12. }  
Step 2
 
Add View

Open a HomeController class -> right click inside Index method -> click Add View.
  1. @ {  
  2.     Layout = null;  
  3. } <  
  4. !DOCTYPE html >  
  5.     <  
  6.     html >  
  7.     <  
  8.     head >  
  9.     <  
  10.     meta name = "viewport"  
  11. content = "width=device-width" / >  
  12.     <  
  13.     title > Index < /title> <  
  14.     /head> <  
  15.     body >  
  16.     <  
  17.     div >  
  18.     <  
  19.     div style = "background:center" >  
  20.     @Html.DropDownList("dropdownname"new List < SelectListItem > {  
  21.         new SelectListItem {  
  22.             Text = "IT", Value = "1"  
  23.         },  
  24.         new SelectListItem {  
  25.             Text = "HR", Value = "2"  
  26.         }  
  27.     }, "select department") <  
  28.     /div> <  
  29.     /div> <  
  30.     /body> <  
  31.     /html>  
Output

DropDownList
Next Recommended Reading DropDownList with Enum in ASP.NET MVC