zeeshan akram

zeeshan akram

  • 1.3k
  • 325
  • 16.6k

MVC Cascading Drop Down Not Selected When Editing

May 17 2020 1:39 AM
When I edit record country list is loaded but the city list not loaded. For example, I have saved record  Country USA  and City are Newyork is not loaded if the USA country selected. This function works well when I add new Add Record But Edit I use in Edit Operation I have the Problem.
 
C#
 
  1.   List<Country> listCountry = _ICountry.listofCountry();  
  2.            ViewBag.CountryList = new SelectList(listCountry, "CountryID""CountryName");  
  3.   
  4. public JsonResult GetCity(int CountryID)  
  5.        {  
  6.            List<City> ctyList = _ILogin.GetCityList(CountryID);  
  7.            return Json(ctyList, JsonRequestBehavior.AllowGet);  
  8.   
  9.        }  
 
  1. <div class="row">  
  2.                                        <div class="col-md-6">  
  3.                                            <label class="col-xs-3 control-label">Country</label>  
  4.                                            <div class="controls col-xs-9">  
  5.                                                @Html.DropDownListFor(m => m.CountryID, ViewBag.CountryList as SelectList, "Choose the Country Name", new { @class = "input-border-btm" })  
  6.   
  7.                                            </div>  
  8.                                        </div>  
  9.                                        <div class="col-md-6">  
  10.                                            <label class="col-xs-3 control-label">City</label>  
  11.                                            <div class="controls col-xs-9">  
  12.                                                @Html.DropDownListFor(m => m.CityID, new SelectList(" "), "Choose City", new { @class = "input-border-btm" })  
  13.   
  14.                                            </div>  
  15.                                        </div>  
  16.                                    </div>  
 Jquery
 
 
  1. <script>  
  2.     $(document).ready(function () {  
  3.         $("#CountryID").change(function () {  
  4.             $.get("/Items/GetCity", { CountryID: $("#CountryID").val() }, function (data) {  
  5.                 $("#CityID").empty();  
  6.                 $.each(data, function (index, row) {  
  7.                     $("#CityID").append("<option value='" + row.CityID + "'>" + row.CityName + "</option>")  
  8.                 });  
  9.             });  
  10.         })  
  11.     });  
  12. </script>  

Answers (1)