Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Selected District not retrieved for employee in edit view get

Selected District not retrieved for employee in edit view get

Selected District not retrieved for employee in edit view get


in link above it 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

in Edit view (get)

CODE --> c#

@model WebCourse.Models.Customemployee2
    Layout = null;

<!DOCTYPE html>

    <meta name="viewport" content="width=device-width" />
    <script src="~/scripts/jquery-1.10.2.js"></script>
        $(function () {
            $("#CountryId").change(function () {
             //  alert("error");
                var x = $(this).val();
                    url: "/empcourse/getcitybyid",
                    data: { id: x },
                        $.each(res, function (i, e) {
                            if (e.Id == $("#cityidhidden").val())
                                $("#citylist").append("<option selected value='" + e.Id + "'>" + e.CityName + "<option>")
                                $("#citylist").append("<option value='" + e.Id + "'>" + e.CityName + "<option>")


            $("#citylist").change(function () {
                // alert("error");
                var y = $(this).val();
                    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>")
                                $("#districtlist").append("<option value='" + e.Id + "'>" + e.DistrictName + "<option>")



        @using (Html.BeginForm())
                <input type="hidden" value="@ViewBag.Cityid" id="cityidhidden" />
                <input type="hidden" value="@ViewBag.dist" id="disthidden" />
                Name:@Html.TextBoxFor(a => a.Name)
                <br />
                <br />
                City:<select id="citylist" name="CityId"></select>
                <br />
                District:<select id="districtlist" name="DistrictId"></select>
                <br />
                <input type="submit" />

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; }


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close