6
Answers

Selected District not retrieved for employee in edit view ge

ahmed salah

ahmed salah

8y
260
1
 
in image above show what i need
I have three drop down list Country,City,District
three drop down list cascade each other
meaning select country fill City and select City fill District in edit or create both
in edit view i can get value selected previous for country and city for employee i need to edit
but District cannot get selected value for employee i need to edit

  1. in Edit view (get)  
  2.   
  3. @model WebCourse.Models.Customemployee2  
  4. @{  
  5.     Layout = null;  
  6. }  
  7.   
  8. <!DOCTYPE html>  
  9.   
  10. <html>  
  11. <head>  
  12.     <meta name="viewport" content="width=device-width" />  
  13.     <title>Edit</title>  
  14.     <script src="~/scripts/jquery-1.10.2.js"></script>  
  15.     <script>  
  16.         $(function () {  
  17.             $("#CountryId").change(function () {  
  18.                 $("#citylist").empty();  
  19.              //  alert("error");  
  20.                 var x = $(this).val();  
  21.                 $.ajax({  
  22.                     url: "/empcourse/getcitybyid",  
  23.                     data: { id: x },  
  24.                     success:function(res)  
  25.                     {  
  26.                         $.each(res, function (i, e) {  
  27.                             if (e.Id == $("#cityidhidden").val())  
  28.                             {  
  29.                                 $("#citylist").append("<option selected value='" + e.Id + "'>" + e.CityName + "<option>")  
  30.                             }  
  31.                             else  
  32.                             {  
  33.                                 $("#citylist").append("<option value='" + e.Id + "'>" + e.CityName + "<option>")  
  34.                             }  
  35.                              
  36.   
  37.                         });  
  38.                     }  
  39.                 });  
  40.   
  41.   
  42.             });  
  43.             $("#CountryId").change();  
  44.             $("#citylist").change(function () {  
  45.                 $("#districtlist").empty();  
  46.                 // alert("error");  
  47.                 var y = $(this).val();  
  48.                 $.ajax({  
  49.                     url: "/empcourse/getdistrictbyid",  
  50.                     data: { id: y },  
  51.                     success: function (res) {  
  52.                         $.each(res, function (i, e) {  
  53.                             if (e.Id == $("#disthidden").val()) {  
  54.                                 $("#districtlist").append("<option selected value='" + e.Id + "'>" + e.DistrictName + "<option>")  
  55.                             }  
  56.                             else  
  57.                             {  
  58.                                 $("#districtlist").append("<option value='" + e.Id + "'>" + e.DistrictName + "<option>")  
  59.   
  60.                             }  
  61.   
  62.                         });  
  63.                     }  
  64.                 });  
  65.   
  66.   
  67.             });  
  68.              
  69.             $("#citylist").change();  
  70.         });  
  71.         </script>  
  72. </head>  
  73. <body>  
  74.     <div>  
  75.         @using (Html.BeginForm())  
  76.         {  
  77.             <div>  
  78.                 <input type="hidden" value="@ViewBag.Cityid" id="cityidhidden" />  
  79.                 <input type="hidden" value="@ViewBag.dist" id="disthidden" />  
  80.                 Name:@Html.TextBoxFor(a => a.Name)  
  81.                 <br />  
  82.                 Country:@Html.DropDownList("CountryId")  
  83.                 <br />  
  84.                 City:<select id="citylist" name="CityId"></select>  
  85.                 <br />  
  86.                 District:<select id="districtlist" name="DistrictId"></select>  
  87.                 <br />  
  88.                 <input type="submit" />  
  89.             </div>  
  90.         }  
  91.   
  92.     </div>  
  93. </body>  
  94. </html>  
  95. in Edit function in controller empcourse  
  96.   
  97.  public class empcourseController : Controller  
  98.     {  
  99.         mycourseEntities db = new mycourseEntities();  
  100.   
  101.         // GET: empcourse  
  102.          
  103.         public ActionResult Edit(int id)  
  104.         {  
  105.             Employee old = db.Employees.Find(id);  
  106.             if (old != null)  
  107.             {  
  108. // country and city working  
  109. //district not working  
  110.                 int countryid = old.Destrict.City.Country.Id;  
  111.                 var vm = new Customemployee2();  
  112.                 vm.Name = old.Name;  
  113.                 ViewBag.CountryId = new SelectList(db.Countries.ToList(), "Id""CountryName",countryid);  
  114.                 ViewBag.Cityid = old.Destrict.City.Id;  
  115.                 ViewBag.dist = old.DistrictId;  
  116.                 return View(vm);  
  117.             }  
  118. //getcitybyid retrieve city it call in ajax in jquery and it working  
  119.         public JsonResult getcitybyid(int id)  
  120.         {  
  121.             db.Configuration.ProxyCreationEnabled = false;  
  122.             return Json(db.Cities.Where(a => a.CountryId == id), JsonRequestBehavior.AllowGet);  
  123.         }  
  124. //getdistrictbyid retrieve district it call in ajax in jquery and it working  
  125.         public JsonResult getdistrictbyid(int id)  
  126.         {  
  127.             db.Configuration.ProxyCreationEnabled = false;  
  128.             return Json(db.Destricts.Where(a => a.CityId == id), JsonRequestBehavior.AllowGet);  
  129.         }  
  130.            
  131.         }  
  132. in model Customemployee2  
  133.   
  134.  public class Customemployee2  
  135.     {  
  136.         public string Name { getset; }  
  137.         public int  DistrictId { getset; }  
  138.   
  139.     }  
  140. }  
Answers (6)