2 Replies - 596 Views - Last Post: 23 August 2016 - 01:48 PM

#1 ahmedba  Icon User is offline

  • D.I.C Head

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

select item from city not fill items in district drop down when create

Posted 17 August 2016 - 07:52 AM

I have 4 table relation in sql server database is good

Country table

Id primarykey increment identity

Countryname

City table

Id primary key increment identity

Cityname

CountryId forignkey

Ditrict table

Id primarykey increment identity

Districtname

CityId forignkey

Employee table

Id primary key increment identity

fname

sname

salary

Bonus

districtid forignkey

Relation not have any proplem

when select items from country it fill city drop down list without any problem

but when i select item from city it not fill items in district why

something wrong in my code i do as following
my controller employee as following

Employee Controller
public class EmployeeController : Controller
{

// GET: Employee
mytaskdbEntities db = new mytaskdbEntities();
public ActionResult Index()
{
return View(db.Employees.ToList());
}
public ActionResult Create()
{
ViewBag.CountryList = new SelectList(db.Countries.ToList(), "Id","Countryname");

return View();
}
public JsonResult getcitybyid(int id)
{
db.Configuration.ProxyCreationEnabled = false;
return Json(db.Cities.Where(a =>a.CountryId == id), JsonRequestbehavior.AllowGet);
}
public JsonResult getdistrictbyid(int id)
{
db.Configuration.ProxyCreationEnabled = false;
return Json(db.Districts.Where(a => a.CityId == id), JsonRequestbehavior.AllowGet);
}
}
}




my Employee view is

@model LinqProject.Models.Employee
@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Create</title>
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script>
$(function () {
$("#CountryList").change(function () {
$("#citylist").empty();
// alert("error");
var x = $(this).val();
$.ajax({
url: "/Employee/getcitybyid",
data: { id: x },
success:function(res)
{
$.each(res, function (i, e) {
$("#citylist").append("<option value='"+e.id+"'>"+e.Cityname+"<option>")

});
}
});


});
$("#citylist").change(function () {
$("#districtlist").empty();
var y = $(this).val();
$.ajax({
url: "/Employee/getdistrictbyid",
data: { id: y },
success: function (res) {
$.each(res, function (i, e) {
$("#districtlist").append("<option value='" + e.id + "'>" + e.Districtname + "<option>")

});
}
});


});
});
</script>
</head>
<body>
<div> 
@using (Html.BeginForm())
{
<div>
FirstName:@Html.TextBoxFor(a=>a.fname)

</div>
<div>
LastName:@Html.TextBoxFor(a => a.sname)

</div>
<div>
Salary:@Html.TextBoxFor(a => a.Salary)

</div>
<div>
Bonus:@Html.TextBoxFor(a => a.Bonus)

</div>
<div>
Bonus:@Html.TextBoxFor(a => a.Active)

</div>
<div>
CountryName:@Html.DropDownList("CountryList")

</div>
<div>
CityName:<select id="citylist" name="CityId"></select>
</div>
<div>
District:<select id="districtlist" name="districtId"></select>
</div>
<input type="submit" />
}
</div>
</body>
</html>




Is This A Good Question/Topic? 0
  • +

Replies To: select item from city not fill items in district drop down when create

#2 ahmedba  Icon User is offline

  • D.I.C Head

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

Re: select item from city not fill items in district drop down when create

Posted 17 August 2016 - 04:46 PM

what is wrong in code above
although database have records for country and city and district
but nothing show in drop down list district
Was This Post Helpful? 0
  • +
  • -

#3 SixOfEleven  Icon User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: select item from city not fill items in district drop down when create

Posted 23 August 2016 - 01:48 PM

It's hard to tell because you mix your cases and the code is badly formatted. If you are not getting a compile error it is probably that the you're trying to grab the wrong property in the view. Most likely you're code is DistrictId and you are using districtId in your view.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1