2
Reply

Cascading DropDownList: Updating an existing value

Luis Alberto Delgado de la Flor

Luis Alberto Delgado de la Flor

Jul 14 2017 3:58 PM
225

I have the following JS which I use to populate a dropdownlist (District) depending on which Department the user chose.

Here the code:

First I populate the Department dropdownlist and I insert a 'Select' value to use it as default:
  1. public IActionResult Create(int? id)  
  2.     {  
  3.         List DepartmentList = new List();  
  4.         DepartmentList = (from department in _context.Departments  
  5.                           select department).ToList();  
  6.         DepartmentList.Insert(0, new Department { DepartmentID = 0, DepartmentName = "Select" });  
  7.         ViewBag.ListofDepartment = DepartmentList;  
  8.         //...etc  
  9.     }  
Then I pass it to the View, which is a PartialView called _Create
  1. form asp-action="Create" role="form">  
  2.   
  3. <div asp-validation-summary="ModelOnly" class="text-danger"></div>  
  4. <div class="modal-body form-horizontal">  
  5.     <div class="form-group">  
  6.         <label asp-for="DepartmentID" class="col-md-2 control-label"></label>  
  7.         <div class="col-md-10">  
  8.             <select asp-for="DepartmentID" class="form-control"  
  9.                     asp-items="@(new SelectList(@ViewBag.ListofDepartment,"DepartmentID","DepartmentName"))"></select>  
  10.         </div>  
  11.     </div>  
  12.     <div class="form-group">  
  13.         <label class="col-md-2 control-label">District</label>  
  14.         <div class="col-md-10">  
  15.             <select class="form-control" id="DistrictID" name="DistrictID" asp-for="DistrictID"  
  16.                     asp-items="@(new SelectList(string.Empty,"DistrictID","DistrictName"))"></select>  
  17.         </div>  
  18.     </div>  
 

Here, I'm setting the District dropdownlist as a Empty list, since I'll populate it with JS.

After that I include the following Js to Populate the District dropdownlist, first with a Select, and then with the values required depending on which Department was chose.

  1. <script src="~/js/store-index.js" asp-append-version="true"></script>  
  2. <script type="text/javascript">  
  3.         $('#modal-action-store').on('shown.bs.modal', function (e) {  
  4.             var items = "<option value='0'>Select</option>";  
  5.             $('#DistrictID').html(items);  
  6.         });  
  7. </script>  
  8. <script type="text/javascript">  
  9.     $('#modal-action-store').on('shown.bs.modal', function (e) {  
  10.         $('#DepartmentID').change(function () {  
  11.             var url = '@Url.Content("~/")' + "Stores/GetDistrict";  
  12.             var ddlsource = "#DepartmentID";  
  13.             $.getJSON(url, { DepartmentID: $(ddlsource).val() }, function (data) {  
  14.                 var items = '';  
  15.                 $("#DistrictID").empty();  
  16.                 $.each(data, function (i, district) {  
  17.                     items += "<option value='" + district.value + "'>" + district.text + "</option>";  
  18.                 });  
  19.                 $('#DistrictID').html(items);  
  20.             });  
  21.         });  
  22.     });  
  23. </script>  
 

This works fine when I create a new Store.

The problem:

When I want to update an existing Store, I can put the Department value inside the dropdownlist, but the District dropdownlist don't populate with the value which that Store has or the other possible values in case the user wants to update this particular Store district.

 

I suspect that I must add some functionality to my JS, but I don't really know how.

IDEA: I suspect that I must change the 2nd part of the JavaScript to:

  • Add a conditional if I'm seeing a new or an existing store.
  • If it is new, do what you are doing now,
    • Else, instead of 'Select', put the corresponding District (and show this) and fill the dropdown with the rest of districts corresponding with that Department.
  • After this point, if the user change the Department dropdown, do what you have been doing up until now.
 

Thanks in advance.

EDIT: Additional info

I'm using a ViewModel since I need the properties from several models: Department, District and Store:

  1. public class StoreIndexData  
  2. {  
  3.     public int DepartmentID { get; set; }  
  4.     public string DepartmentName { get; set; }  
  5.   
  6.     public int DistrictID { get; set; }  
  7.     public string DistrictName { get; set; }  
  8.   
  9.     public int StoreID { get; set; }  
  10.     public int StoreChainID { get; set; }  
  11.     public string StoreName { get; set; }  
  12.     public string StoreAddress { get; set; }  
  13.     public int StoreArea { get; set; }  
  14. }  
Finally, this is the Json I use in the second JScript to grab the list of Districts once the Department has been choose in the creation of a new store:
  1. public JsonResult GetDistrict(int DepartmentID)  
  2.     {  
  3.         List DistrictList = new List();  
  4.         DistrictList = (from district in _context.Districts  
  5.                         where district.DepartmentID == DepartmentID  
  6.                         select district).ToList();  
  7.         DistrictList.Insert(0, new District { DistrictID = 0, DistrictName = "Select" });  
  8.         return Json(new SelectList(DistrictList, "DistrictID""DistrictName"));  
  9.     }  
Thanks for any reply that helps! 

Answers (2)