Cascading Dropdownlist & ViewModel

Jun 27 2017 2:57 PM

Hello,

I have the following models: Department (as city), District and Store.

Department has one to many Districts
Districts has one to many Stores

I'm constructing a view where I create a new Store and the user can choose the Department(city) and then the District dropdownlist updates instantly with the corresponding districts.

For this I created this ViewModel: StoreIndexData

  1. public class StoreIndexData   
  2. {   
  3. public Store Stores { get; set; }   
  4. public Department Departments { get; set; }   
  5. }  

This is the Controller where I populate the dropdownlist for the Department:

  1. public IActionResult Create() {   
  2.    //Departaments List   
  3.    List Departmentlist = _context.Departments.ToList();   
  4.    Departmentlist.Insert(0, new Department { DepartmentID = 0,   
  5.                                  DepartmentName = "Select" });  
  6.  ViewBag.DepartmentList = new SelectList(Departmentlist,   
  7.                                  "DepartmentID""DepartmentName");   
  8. return View(); }  

And this is the View:

  1. @model Application.Models.ApplicationviewModels.StoreIndexData  
  2.   
  3. <form asp-action="Create">  
  4. <div class="form-horizontal">  
  5.     <h4>Store</h4>  
  6.     <hr />  
  7.     <div asp-validation-summary="ModelOnly" class="text-danger"></div>  
  8.   
  9.     <div class="form-group">  
  10.         <label asp-for="Departments.DepartmentID" class="col-md-2 control-label"></label>  
  11.         <div class="col-md-10">  
  12.             <select asp-for="Departments.DepartmentID"   
  13.              class="form-control" asp-items="ViewBag.DepartmentList"></select>  
  14.         </div>  
  15.     </div>  
  16.   
  17.     <div class="form-group">  
  18.         <label asp-for="Stores.DistrictID" class="col-md-2 control-label"></label>  
  19.         <div class="col-md-10">  
  20.             @Html.DropDownListFor(model => model.Stores.DistrictID, new SelectList(" "), "Select"new { @class = "form-control"})  
  21.         </div>  
  22.     </div>  
 

And in the end of the view, this is my JS section:

  1. <script src="~/js/jquery-3.2.1.min.js"></script>  
  2. <script>  
  3.     $(document).ready(function () {  
  4.         $("#DepartmentID").change(function () {  
  5.             $.get("/Stores/GetDistrictList", { DepartmentID: $("#DepartmentID").val() }, function (data) {  
  6.                 $("#DistrictID").empty();  
  7.                 $.each(data, function (index, row) {  
  8.                     $("#DistrictID").append("<option value='" + row.DepartmentID + "'>" + row.DepartmentName + "</option>")  
  9.                 });  
  10.             });  
  11.         })  
  12.     });  
  13. </script>  
 

This section calls this action:

  1.     public JsonResult GetDistrictList(int DepartmentID)  
  2. {  
  3.     List DistrictList = _context.Districts.Where(x => x.DepartmentID == DepartmentID).ToList();  
  4.     return Json(DistrictList);  
  5. }  

And that's it.

When I run the application, well, the District dropdownlist don't populate

_Questions:

  • Is it possible that I can't access this properties inside the JScript

    "#DepartmentID"
    like this since I'm using a viewmodel? Maybe I need to change it to something like "#Departments.DepartmentID"?
  • Could this work using a ViewModel? I'm not adding the Department property inside the Store model because it would be irrelevant since it gets its Department thru the District.

  • I saw in the example that I followed that they added this line ProxyCreationEnabled = false Inside the JsonResult action. I didn't add it because I don't know what it does and don't know the consecuences in my application if I add it.

Thanks in advance for any help.

Regards,

_Update:

I put a break point on this line, inside the JsonResult:

  1. List<District> DistrictList = _context.Districts.
  2.       Where(x => x.DepartmentID == DepartmentID).ToList();  

But the application did not stopped there when I used the Department dropdownlist that is supossed to call that JsonResult. I believe the script section is what might be wrong.

 

Answers (2)