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
- edit view District
- @model WebCourse.Models.Destrict
- @{
- Layout = null;
- }
-
- <!DOCTYPE html>
-
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Edit</title>
- <script src="~/scripts/jquery-1.10.2.js"></script>
- <script>
- $(function () {
- $("#ctry").change(function () {
- $("#citylist").empty();
- var x = $(this).val();
- $.ajax({
- url: "/empcourse/getcitybyid",
- data: { id: x },
- success:function(res)
- {
-
- $.each(res, function (i, e) {
-
- $("#citylist").append("<option value='" + e.Id + "'>" + e.CityName + "<option>")
-
-
- });
- }
- });
-
-
- });
- $("#ctry").change();
-
-
- });
- </script>
- </head>
- <body>
- <div>
- @using (Html.BeginForm())
- {
- <div>
- CountryName:@Html.DropDownList("ctry")
- </div>
- <div>
- City:<select id="citylist" name="CityId"></select>
- <br />
- DistrictName:@Html.TextBoxFor(a => a.DistrictName)
- </div>
-
-
-
- <input type="submit" />
-
- }
- </div>
-
- </body>
- </html>
- in district controller in edit (get)
-
-
- namespace WebCourse.Controllers
- {
- public class DistrictController : Controller
- {
- mycourseEntities db = new mycourseEntities();
- public ActionResult Edit(int id)
- {
-
- Destrict D = db.Destricts.Find(id);
- int ctryold = D.City.Country.Id;
-
- ViewBag.ctry = new SelectList(db.Countries.ToList(), "Id", "CountryName", ctryold);
- return View(D);
-
-
- }
- public JsonResult getcitybyid(int id)
- {
- db.Configuration.ProxyCreationEnabled = false;
- return Json(db.Cities.Where(a => a.CountryId == id), JsonRequestBehavior.AllowGet);
- }
- }
- }