We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Selected District not retrieved for employee in edit view get

Thread ID:

Created:

Updated:

Platform:

Replies:

125609 Aug 27,2016 11:15 PM Aug 31,2016 12:01 AM ASP.NET MVC 2
loading
Tags: DropDownList
ahmedsa
Asked On August 27, 2016 11:15 PM

cannot retrieve district in edit view(district is empty)
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

in Edit view (get)

@model WebCourse.Models.Customemployee2
@{
    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 () {
            $("#CountryId").change(function () {
                $("#citylist").empty();
             //  alert("error");
                var x = $(this).val();
                $.ajax({
                    url: "/empcourse/getcitybyid",
                    data: { id: x },
                    success:function(res)
                    {
                        $.each(res, function (i, e) {
                            if (e.Id == $("#cityidhidden").val())
                            {
                                $("#citylist").append("<option selected value='" + e.Id + "'>" + e.CityName + "<option>")
                            }
                            else
                            {
                                $("#citylist").append("<option value='" + e.Id + "'>" + e.CityName + "<option>")
                            }
                           

                        });
                    }
                });


            });
            $("#CountryId").change();
            $("#citylist").change(function () {
                $("#districtlist").empty();
                // alert("error");
                var y = $(this).val();
                $.ajax({
                    url: "/empcourse/getdistrictbyid",
                    data: { id: y },
                    success: function (res) {
                        $.each(res, function (i, e) {
                            if (e.Id == $("#disthidden").val()) {
                                $("#districtlist").append("<option selected value='" + e.Id + "'>" + e.DistrictName + "<option>")
                            }
                            else
                            {
                                $("#districtlist").append("<option value='" + e.Id + "'>" + e.DistrictName + "<option>")

                            }

                        });
                    }
                });


            });
           
            $("#citylist").change();
        });
        </script>
</head>
<body>
    <div>
        @using (Html.BeginForm())
        {
            <div>
                <input type="hidden" value="@ViewBag.Cityid" id="cityidhidden" />
                <input type="hidden" value="@ViewBag.dist" id="disthidden" />
                Name:@Html.TextBoxFor(a => a.Name)
                <br />
                Country:@Html.DropDownList("CountryId")
                <br />
                City:<select id="citylist" name="CityId"></select>
                <br />
                District:<select id="districtlist" name="DistrictId"></select>
                <br />
                <input type="submit" />
            </div>
        }

    </div>
</body>
</html>
in Edit function in controller empcourse

 public class empcourseController : Controller
    {
        mycourseEntities db = new mycourseEntities();

        // GET: empcourse
       
        public ActionResult Edit(int id)
        {
            Employee old = db.Employees.Find(id);
            if (old != null)
            {
// country and city working
//district not working
                int countryid = old.Destrict.City.Country.Id;
                var vm = new Customemployee2();
                vm.Name = old.Name;
                ViewBag.CountryId = new SelectList(db.Countries.ToList(), "Id", "CountryName",countryid);
                ViewBag.Cityid = old.Destrict.City.Id;
                ViewBag.dist = old.DistrictId;
                return View(vm);
            }
//getcitybyid retrieve city it call in ajax in jquery and it working
        public JsonResult getcitybyid(int id)
        {
            db.Configuration.ProxyCreationEnabled = false;
            return Json(db.Cities.Where(a => a.CountryId == id), JsonRequestBehavior.AllowGet);
        }
//getdistrictbyid retrieve district it call in ajax in jquery and it working
        public JsonResult getdistrictbyid(int id)
        {
            db.Configuration.ProxyCreationEnabled = false;
            return Json(db.Destricts.Where(a => a.CityId == id), JsonRequestBehavior.AllowGet);
        }
         
        }
in model Customemployee2

 public class Customemployee2
    {
        public string Name { get; set; }
        public int  DistrictId { get; set; }

    }
}

ahmedsa
Replied On August 28, 2016 04:08 AM

can any one try to help me to solve this point

Dhivyalakshmi Thirumurugan [Syncfusion]
Replied On August 31, 2016 12:01 AM

 
Thanks for contacting Syncfusion support. 
 
We have checked your code and understood that you have problems in using Html helper for DropDowList. If you have used Syncfusion DropDownList means,  then we have a property named as cascadeTo and we can achieve the cascading operation using that property without performing complex operations. Please find the references below. 
 
 
Please let us know if you have any queries related to Syncfusion components. 
 
Regards, 
Dhivyalakshmi. 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;