Search in Help for developer site.

Monday 2 January 2017

The Various Ways of Binding MVC DropDownList

To Bind a DropDownListFor or DropDown in asp.net MVC from database, from enum or from hard coded values, most people suggest to add a property in the model with IEnumarable or IList. We can use it but not very helpful because after every postback we will need to fill the list again otherwise it will be null. So using ViewBag is not a bad idea, we will use it in our article.


    What we will see in this discussion:
    1. Bind DropDown with hard coded values
    2. Bind DropDown with records from database
    3. Bind DropDown with Enum
    4. Bind DropDown on change of other DropDown's value

    1. Bind DropDown with hard coded values

    We will try to write both controller code and view in every case, so let's see our first example by using hard coded value
    1.     public ActionResult Index()
    2.     {      
    3.         ViewBag.Active = new List<SelectListItem> {                  
    4.                  new SelectListItem { Text = "Yes", Value = "1"},                  
    5.                  new SelectListItem { Text = "No", Value = "0"}
    6.              };
    7.         var model = new ProductModel();
    8.         return View(model);
    9.     }
    In the view, I am not going to show all the label and other model detail. Here IsActie is the model value which will come from database for existing records otherwise null or default value from the model, I used the class name from bootstrap.
    1. @Html.DropDownListFor(model => model.IsActive,
    2.    new SelectList(ViewBag.Active, "Value", "Text"),
    3.    "Select status",
    4.     new { @class = "form-control" })
    If it is hard coded values and is a big list then we can create a separate method to create the list otherwise directly we can declare it in view rather than passing from controller view as we did in our above example, see how
    1. @Html.DropDownListFor(model => model.Category,
    2.     new SelectList(new List<SelectListItem> {                  
    3.              new SelectListItem { Text = "Yes", Value = "1"},                  
    4.              new SelectListItem { Text = "No", Value = "0"}
    5.          }, "Value", "Text"),
    6.     "Select status",
    7.      new { @class = "form-control" })
    It doesn't looks good but good thing is that we don't need to create the list again and again on postback, which we will see latter.

    2. Bind DropDown with records from database

    If you will explore a little bit, you will found most of the people suggest to convert the record into SelectList in controller or in model before passing to view. Is that really required or is a good practice? I cannot say about others but for me, it is really frustrating to convert my table into SelectList every time. If not then is there any way to directly bind the dropdown from a table records, yes, we will see it in this example:
    I have a table category which we will use in this example, here is the table structure:
    1. public partial class Category
    2. {
    3.     public int CategoryId { get; set; }
    4.     public string CategoryName { get; set; }
    5.     public bool IsActive { get; set; }
    6. }
    Controller code:
    1. // GET: Products
    2. public ActionResult Create()
    3. {
    4.     var db = new MyDBEntities();
    5.     ViewBag.Categories = db.Categories.Where(x => x.IsActive);
    6.     var model = new ProductModel();
    7.     return View(model);
    8. }  
    9. // POST: Create Product
    10. public ActionResult Create(ProductModel model)
    11. {
    12.     var db = new MyDBEntities();
    13.     if (ModelState.IsValid)
    14.     {
    15.         // code to save record  and redirect to listing page
    16.     }
    17.     // If model is not valid then reload categories from database
    18.     ViewBag.Categories = db.Categories.Where(x => x.IsActive);
    19.     return View(model);
    20. }
    In our GET action we are fetching all active categories from database and passing to view by using ViewBag. In POST action we try to validate the posted data by using ModelState.IsValid method, if valid, save data and redirect user to your desired page otherwise, re-populate the the categories before returning the user to page to fix the issue. Now we will see our view html
    1. @Html.DropDownListFor(model => model.Category,
    2.     new SelectList(ViewBag.Categories, "CategoryID", "CategoryName"),
    3.     "Select Category",
    4.     new { @class = "form-control" })
    As you noticed, we directly pass the table records to view after fetching from database without converting to Select List and converted into Select List directly into the view, which is easy and save time. If we will do the same in controller then we need to convert the list in both GET as well as POST action.

    3. Bind DropDown with Enum

    There are two ways to bind and save the records, and it's all depends on requirement, whether we want to save the number or text value of the enum, we will try both. Let's first create an enum say Language, I am adding those language which I read write and speak
    1. public enum Language
    2. {
    3.     Arabic,
    4.     English,
    5.     Hindi,
    6.     Spanish,
    7.     Urdu
    8. }
    If we need to save the text or say bind with text then
    1. // In controller
    2. ViewBag.Languages = Enum.GetValues(typeof(Language)).Cast<Language>();
    3. // In View
    4. @Html.DropDownListFor(model => model.Language,
    5.         new SelectList(ViewBag.Languages),
    6.         "Select language",
    7.         new { @class = "form-control" })
    As in most cases we want to use value and text, so we need to create a structure or class and create the list of it from enum which we will use in this example:
    We will create a structure which we can use for entire application
    1. public struct DDL
    2. {
    3.     public int Value { get; set; }
    4.     public String Text { get; set; }
    5. }
    Now create the list of DLL by using Language enum foreach, and assign to ViewBag.Languages
    1. var languages = new List<DDL>();
    2. foreach (Language lang in Enum.GetValues(typeof(Language)))
    3.     languages.Add(new DDL { Value = (int)lang, Text= lang.ToString() });
    4. ViewBag.Languages = languages;
    In view, binding is same as earlier:
    1. @Html.DropDownListFor(model => model.Language,
    2.       new SelectList(ViewBag.Languages, "Value", "Text"),
    3.       "Select status",
    4.       new { @class = "form-control" })
    Now when we will post the page, it will post the language Id not the text.

    4. Bind DropDown on change of other DropDown's value

    we will try to bind child Dropdown on selection change of parent Dropdown, say Country to State or Sate to City, by using jQuery which will call an action in controller and return JSON for selected id, and with the help of jQuery append values to child Dropdown. We will try to create page to add new employee and state and city will be used as parent and child Dropdown.

    Let's see the tables we are going to use:
    1. public partial class State
    2. {
    3.     public int StateId { get; set; }
    4.     public string StateName { get; set; }
    5.     public string Abbr { get; set; }
    6. }
    7. public partial class City
    8. {
    9.     public int CityId { get; set; }
    10.     public string CityName { get; set; }
    11.     public int StateId { get; set; }
    12. }
    Employee Model:
    1. public class Employees
    2. {
    3.     [Key]
    4.     public int EmployeeId { get; set; }
    5.     [Required, Display(Name="Employee Name")]
    6.     public string EmployeeName { get; set; }
    7.     [Required, Display(Name = "Address")]
    8.     public String Address { get; set; }
    9.     [Required, Display(Name = "State")]
    10.     public int State { get; set; }
    11.     [Required, Display(Name = "City")]
    12.     public int City { get; set; }
    13.     [Display(Name = "Zip Code")]
    14.     public String ZipCode { get; set; }
    15.     [Required, Display(Name = "Phone #")]
    16.     public String Phone { get; set; }
    17. }
    As you can see, I have not added anything in our model for list of states or cities because we are using Code first, which will create the database tables, we will try to get data from database and pass to view by using ViewBag, here is our HttpGet and HttpPost action method:
    1. [HttpGet]
    2. public ActionResult Create()
    3. {
    4.     ViewBag.StateList = db.States;
    5.     var model = new Employees();
    6.     return View(model);
    7. }
    8. [HttpPost]
    9. public ActionResult Create(Employees model)
    10. {
    11.     if (ModelState.IsValid)
    12.     {
    13.         // code to save record  and redirect to listing page
    14.     }
    15.     ViewBag.StateList = db.States;
    16.     return View(model);
    17. }
    HTML code for both state and city dropdown:
    1. <div class="form-group">
    2.     @Html.LabelFor(m => m.State,  new { @class = "control-label col-md-2" })
    3.     <div class="col-md-10">
    4.         @Html.DropDownListFor(m => m.State,
    5.               new SelectList(ViewBag.StateList, "StateId", "StateName"),
    6.               "Select state",
    7.               new { @class = "form-control", @onchange="FillCity()" })
    8.         @Html.ValidationMessageFor(m => m.State, "", new { @class = "text-danger" })
    9.     </div>
    10. </div>
    11. <div class="form-group">
    12.     @Html.LabelFor(m => m.City, new { @class = "control-label col-md-2" })
    13.     <div class="col-md-10">
    14.         @Html.DropDownListFor(m => m.City,
    15.        new SelectList(Enumerable.Empty<SelectListItem>(), "CityId", "CityName"),
    16.               "Select city",
    17.               new { @class = "form-control" })
    18.         @Html.ValidationMessageFor(m => m.City, "", new { @class = "text-danger" })
    19.     </div>
    20. </div>
    As you can notice we used @onchange="FillCity()" in our State dropdown so we need a JavaScript function which will be call on change of state. If you are from asp.net background then forgot about the postback, in mvc there is no postback concept so we don't need viewstate of controls so the pages are light weight. Here is our FillCity function:
    1. @section Scripts {
    2. <script>
    3.   function FillCity() {
    4.     var stateId = $('#State').val();
    5.     $.ajax({
    6.         url: '/Employees/FillCity',
    7.         type: "GET",
    8.         dataType: "JSON",
    9.         data: { State: stateId},
    10.         success: function (cities) {                    
    11.             $("#City").html(""); // clear before appending new list
    12.             $.each(cities, function (i, city) {
    13.                 $("#City").append(
    14.                     $('<option></option>').val(city.CityId).html(city.CityName));
    15.             });
    16.         }
    17.     });
    18.   }
    19. </script>
    20. }
    Here we used $.each to loop the items and add item by item to city dropdown. We used to call FillCity action method in our Employees controller.We need a separate action method which can return cities on the basis of selection change and return JSON string.
    1. public ActionResult FillCity(int state)
    2. {
    3.     var cities = db.Cities.Where(c => c.StateId == state);
    4.     return Json(cities, JsonRequestBehavior.AllowGet);
    5. }
    See how we can return json from any action method in our controller. If you want to see the complete html of the page then it is here.
    1. @model MyApp.Models.Employees
    2. @{
    3.     ViewBag.Title = "Index";
    4.     Layout = "~/Views/Shared/_Layout.cshtml";
    5. }
    6. <h2>Index</h2>
    7. @using (Html.BeginForm())
    8. {
    9. @Html.AntiForgeryToken()
    10. <div class="form-horizontal">
    11.     <h4>New Employee</h4>      
    12.     <div class="form-group">
    13.         @Html.LabelFor(m => m.EmployeeName, new { @class = "control-label col-md-2" })
    14.         <div class="col-md-10">
    15.             @Html.TextBoxFor(m => m.EmployeeName, new { @class = "form-control" })
    16.             @Html.ValidationMessageFor(m => m.EmployeeName, "", new { @class = "text-danger" })
    17.         </div>
    18.     </div>
    19.     <div class="form-group">
    20.         @Html.LabelFor(m => m.Address, new { @class = "control-label col-md-2" })
    21.         <div class="col-md-10">
    22.             @Html.TextBoxFor(m => m.Address, new { @class = "form-control" })
    23.             @Html.ValidationMessageFor(m => m.Address, "", new { @class = "text-danger" })
    24.         </div>
    25.     </div>
    26.     <div class="form-group">
    27.         @Html.LabelFor(m => m.State,  new { @class = "control-label col-md-2" })
    28.         <div class="col-md-10">
    29.             @Html.DropDownListFor(m => m.State,
    30.                   new SelectList(ViewBag.StateList, "StateId", "StateName"),
    31.                   "Select state",
    32.                   new { @class = "form-control", @onchange="FillCity()" })
    33.             @Html.ValidationMessageFor(m => m.State, "", new { @class = "text-danger" })
    34.         </div>
    35.     </div>
    36.     <div class="form-group">
    37.         @Html.LabelFor(m => m.City, new { @class = "control-label col-md-2" })
    38.         <div class="col-md-10">
    39.             @Html.DropDownListFor(m => m.City,
    40.            new SelectList(Enumerable.Empty<SelectListItem>(), "CityId", "CityName"),
    41.                   "Select city",
    42.                   new { @class = "form-control" })
    43.             @Html.ValidationMessageFor(m => m.City, "", new { @class = "text-danger" })
    44.         </div>
    45.     </div>
    46.     <div class="form-group">
    47.         @Html.LabelFor(m => m.ZipCode, htmlAttributes: new { @class = "control-label col-md-2" })
    48.         <div class="col-md-10">
    49.             @Html.TextBoxFor(m => m.ZipCode,  new { @class = "form-control" })
    50.             @Html.ValidationMessageFor(m => m.ZipCode, "", new { @class = "text-danger" })
    51.         </div>
    52.     </div>
    53.     <div class="form-group">
    54.         @Html.LabelFor(m => m.Phone, new { @class = "control-label col-md-2" })
    55.         <div class="col-md-10">
    56.             @Html.TextBoxFor(m => m.Phone, new { @class = "form-control" })
    57.             @Html.ValidationMessageFor(m => m.Phone, "", new { @class = "text-danger" })
    58.         </div>
    59.     </div>
    60.     <div class="form-group">
    61.         <div class="col-md-offset-2 col-md-10">
    62.             <input type="submit" value="Create" class="btn btn-success" />
    63.             <a href="/Employees" class="btn btn-warning" >Cancel</a>
    64.         </div>
    65.     </div>
    66. </div>
    67. }
    If you want to add more column value into dropdown text you can concatenate them easily, say we want to show both Id and text in city drop down then we simmply show it like this:
    $('<option></option>').
       val(city.CityId).html(city.CityId + " - " + city.CityName)); 
    Whatever we see here is not complete to maintain the selected value if we found the model invalid in our post method and return back to the view then current selected state's cities will be lost same will happen when we need to fill and select the value in city dropdown for a saved records. We need to add one more JavaScript method to check on document.ready if there is any value already selected in state dropdown then call the FillCity method and fill our city dropdown and selected the city value which we get from the model.
    I am leaving it for you so you can do a little bit practice, hope you will easily complete it, best of luck.

    6 comments:

    1. Great !! ThumbsUPP for this post..keep it up

      ReplyDelete
    2. There is a one more method to Bind Dropdown. Create a Helper Method For Dropdown then you won't need ViewBag for Binding.

      ReplyDelete
    3. saya ingin membuat onchange bagaimana caranya

      ReplyDelete
    4. I want to make an exchange how to make

      ReplyDelete