Today, we are going to learn how to use DropDownList in MVC. We will also see how to set selected Item to ViewBag and show in DropDownList.

Remember
  • While creating ViewBag, if our ViewBag name is the same as the Control that we used in HTML page, then we have no need to specify that ViewBag in HTML.
  • It will automatically fetch particular ViewBag data to particular Controls.
  • So, it is a best practice to keep ViewBag name the same as HTML Control, so as to save time and reduce code complexity.
Let's start MVC DropDownList implementation.

STEP 1 - Create Controller DropDownController

Note - For demo purposes, I used a particular Controller for better understanding. You can use DropDownController Code as per your requirement.

See the code to initialize DropDownList.
DropDownController
  1. namespace MVCApplicationDropDownList.Controllers
  2. {
  3. public class DropDownController : Controller
  4. {
  5. //DropDown : SelectListItem Define for Month & Year DropDownList.
  6. List<SelectListItem> ddlMonths = new List<SelectListItem>();
  7. List<SelectListItem> ddlYears = new List<SelectListItem>();
  8. // GET: DropDown
  9. public ActionResult Index(int? Year) {...}
  10. //DropDown : GetYears() will fill Year DropDown and Return List.
  11. private SelectList GetYears(int? iSelectedYear) {...}
  12. //DropDown : GetMonths() will fill Months as per selected Year and Return List.
  13. private SelectList GetMonths(int? iSelectedYear) {...}
  14. }
  15. }
Index Method
  1. // GET: DropDown
  2. public ActionResult Index(int? Year)
  3. {
  4. if (Year == null)
  5. {
  6. Year = DateTime.Now.Year;
  7. }
  8. ViewBag.linktoYearId = GetYears(Year);
  9. ViewBag.linktoMonthId = GetMonths(Year);
  10. return View();
  11. }
Year Method
  1. //DropDown : GetYears() will fill Year DropDown and Return List.
  2. private SelectList GetYears(int? iSelectedYear)
  3. {
  4. int CurrentYear = DateTime.Now.Year;
  5. for (int i = 2010; i <= CurrentYear; i++)
  6. {
  7. ddlYears.Add(new SelectListItem
  8. {
  9. Text = i.ToString(),
  10. Value = i.ToString()
  11. });
  12. }
  13. //Default It will Select Current Year
  14. return new SelectList(ddlYears, "Value", "Text", iSelectedYear);
  15. }
Month Method
  1. //DropDown : GetMonths() will fill Months as per selected Year and Return List.
  2. private SelectList GetMonths(int? iSelectedYear)
  3. {
  4. var months = Enumerable.Range(1, 12).Select(i => new
  5. {
  6. A = i,
  7. B = DateTimeFormatInfo.CurrentInfo.GetMonthName(i)
  8. });
  9. int CurrentMonth = 1; //January
  10. if(iSelectedYear == DateTime.Now.Year)
  11. {
  12. CurrentMonth = DateTime.Now.Month;
  13. months = Enumerable.Range(1, CurrentMonth).Select(i => new
  14. {
  15. A = i,
  16. B = DateTimeFormatInfo.CurrentInfo.GetMonthName(i)
  17. });
  18. }
  19. foreach (var item in months)
  20. {
  21. ddlMonths.Add(new SelectListItem { Text = item.B.ToString(), Value = item.A.ToString() });
  22. }
  23. //Default It will Select Current Month
  24. return new SelectList(ddlMonths, "Value", "Text", CurrentMonth);
  25. }

STEP 2 Add Index View

Just right click on Controller Index() method and add Index View().


Add the following JavaScript to Index.cshtml.
  1. <script type="text/javascript">
  2. function ChangeYear(obj) {
  3. window.location.href = '@Url.Action("Index","DropDown")?Year=' + obj.value;
  4. }
  5. </script>
Add DropDownList to Index.cshtml.
  1. @using (Html.BeginForm())
  2. {
  3. <div class="row" style="border:solid; border-width:thin; padding:20px; height:400px;">
  4. <div class="col-lg-12 text-center">
  5. <div class="row">
  6. <div class="col-lg-6" align="right">
  7. @Html.DropDownList("linktoMonthId", null, "--Select--", new
  8. {
  9. @id = "ddlMonths",
  10. @class = "form-control",
  11. @style = "width:200px"
  12. })
  13. </div>
  14. <div class="col-lg-6" align="left">
  15. @Html.DropDownList("linktoYearId", null, "--Select--", new
  16. {
  17. @id = "ddlYears",
  18. @onChange = "return ChangeYear(this)",
  19. @class = "form-control",
  20. @style = "width:200px"
  21. })
  22. </div>
  23. </div>
  24. </div>
  25. </div>
  26. }
Now, just clean & build solution. Press F5.
Screen 1
Screen 2

OK, that's it. Hope this tutorial makes sense regarding implementation of Dropdownlist in ASP.NET MVC.