ahmed salah

ahmed salah

  • NA
  • 530
  • 141.8k

Retrieve wrong city for district

Aug 31 2016 5:05 PM
in image above it retrieve wrong city for district
when i need to edit for id 4 in table district as image
it give me wrong city
country name and district name is retrieve true
  1. edit view District  
  2. @model WebCourse.Models.Destrict  
  3. @{  
  4. Layout = null;  
  5. }  
  6.   
  7. <!DOCTYPE html>  
  8.   
  9. <html>  
  10. <head>  
  11. <meta name="viewport" content="width=device-width" />  
  12. <title>Edit</title>  
  13. <script src="~/scripts/jquery-1.10.2.js"></script>  
  14. <script>  
  15. $(function () {  
  16. $("#ctry").change(function () {  
  17. $("#citylist").empty();  
  18. var x = $(this).val();  
  19. $.ajax({  
  20. url: "/empcourse/getcitybyid",  
  21. data: { id: x },  
  22. success:function(res)  
  23. {  
  24.   
  25. $.each(res, function (i, e) {  
  26.   
  27. $("#citylist").append("<option value='" + e.Id + "'>" + e.CityName + "<option>")  
  28.   
  29.   
  30. });  
  31. }  
  32. });  
  33.   
  34.   
  35. });  
  36. $("#ctry").change();  
  37.   
  38.   
  39. });  
  40. </script>  
  41. </head>  
  42. <body>  
  43. <div>  
  44. @using (Html.BeginForm())  
  45. {  
  46. <div>  
  47. CountryName:@Html.DropDownList("ctry")  
  48. </div>  
  49. <div>  
  50. City:<select id="citylist" name="CityId"></select>  
  51. <br />  
  52. DistrictName:@Html.TextBoxFor(a => a.DistrictName)  
  53. </div>  
  54.   
  55.   
  56.   
  57. <input type="submit" />  
  58.   
  59. }  
  60. </div>  
  61.   
  62. </body>  
  63. </html>  
  64. in district controller in edit (get)  
  65.   
  66.   
  67. namespace WebCourse.Controllers  
  68. {  
  69. public class DistrictController : Controller  
  70. {  
  71. mycourseEntities db = new mycourseEntities();  
  72. public ActionResult Edit(int id)  
  73. {  
  74.   
  75. Destrict D = db.Destricts.Find(id);  
  76. int ctryold = D.City.Country.Id;  
  77.   
  78. ViewBag.ctry = new SelectList(db.Countries.ToList(), "Id""CountryName", ctryold);  
  79. return View(D);  
  80.   
  81.   
  82. }  
  83. public JsonResult getcitybyid(int id)  
  84. {  
  85. db.Configuration.ProxyCreationEnabled = false;  
  86. return Json(db.Cities.Where(a => a.CountryId == id), JsonRequestBehavior.AllowGet);  
  87. }  
  88. }  
  89. }  

Answers (8)