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
- namespace MVCApplicationDropDownList.Controllers
- {
- public class DropDownController : Controller
- {
-
-
- List<SelectListItem> ddlMonths = new List<SelectListItem>();
- List<SelectListItem> ddlYears = new List<SelectListItem>();
-
-
- public ActionResult Index(int? Year) {...}
-
-
-
- private SelectList GetYears(int? iSelectedYear) {...}
-
-
-
- private SelectList GetMonths(int? iSelectedYear) {...}
- }
- }
Index Method
-
- public ActionResult Index(int? Year)
- {
- if (Year == null)
- {
- Year = DateTime.Now.Year;
- }
-
- ViewBag.linktoYearId = GetYears(Year);
- ViewBag.linktoMonthId = GetMonths(Year);
- return View();
- }
Year Method
-
- private SelectList GetYears(int? iSelectedYear)
- {
- int CurrentYear = DateTime.Now.Year;
-
- for (int i = 2010; i <= CurrentYear; i++)
- {
- ddlYears.Add(new SelectListItem
- {
- Text = i.ToString(),
- Value = i.ToString()
- });
- }
-
-
- return new SelectList(ddlYears, "Value", "Text", iSelectedYear);
-
- }
Month Method
-
- private SelectList GetMonths(int? iSelectedYear)
- {
- var months = Enumerable.Range(1, 12).Select(i => new
- {
- A = i,
- B = DateTimeFormatInfo.CurrentInfo.GetMonthName(i)
- });
-
- int CurrentMonth = 1;
-
- if(iSelectedYear == DateTime.Now.Year)
- {
- CurrentMonth = DateTime.Now.Month;
- months = Enumerable.Range(1, CurrentMonth).Select(i => new
- {
- A = i,
- B = DateTimeFormatInfo.CurrentInfo.GetMonthName(i)
- });
- }
-
- foreach (var item in months)
- {
- ddlMonths.Add(new SelectListItem { Text = item.B.ToString(), Value = item.A.ToString() });
- }
-
-
- return new SelectList(ddlMonths, "Value", "Text", CurrentMonth);
-
- }
STEP 2 Add Index View
Just right click on Controller Index() method and add Index View().
Add the following JavaScript to Index.cshtml.
- <script type="text/javascript">
-
- function ChangeYear(obj) {
- window.location.href = '@Url.Action("Index","DropDown")?Year=' + obj.value;
- }
-
- </script>
Add DropDownList to Index.cshtml.
- @using (Html.BeginForm())
- {
- <div class="row" style="border:solid; border-width:thin; padding:20px; height:400px;">
- <div class="col-lg-12 text-center">
- <div class="row">
- <div class="col-lg-6" align="right">
- @Html.DropDownList("linktoMonthId", null, "--Select--", new
- {
- @id = "ddlMonths",
- @class = "form-control",
- @style = "width:200px"
- })
- </div>
- <div class="col-lg-6" align="left">
- @Html.DropDownList("linktoYearId", null, "--Select--", new
- {
- @id = "ddlYears",
- @onChange = "return ChangeYear(this)",
- @class = "form-control",
- @style = "width:200px"
- })
- </div>
- </div>
- </div>
- </div>
- }
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.