0 Replies - 355 Views - Last Post: 31 August 2016 - 04:49 PM

#1 ahmedba  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 141
  • Joined: 24-January 14

Retrieve wrong city for district

Posted 31 August 2016 - 04:49 PM

Posted Image
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);
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Page 1 of 1