选择项目从城市不填充项目在区域下拉创建时

时间:2016-08-17 22:10:38

标签: linq

我有4张桌子

国家/地区表

Id primarykey增量标识

COUNTRYNAME

城市表

Id主键增量标识

CITYNAME

CountryId forignkey

Ditrict表

Id primarykey增量标识

Districtname

CityId forignkey

员工表

Id主键增量标识

FNAME

SNAME

薪水

加成

districtid forignkey

关系没有任何问题,并且sql server数据库中的关系是好的

当从国家/地区选择项目时,它会毫无问题地填写城市下拉列表

但是当我从城市中选择项目时,它不会填充区域中的项目

我的代码中出现了错误,如下所示 我的控制员员如下:

员工控制器

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

我的员工视图是

@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>

0 个答案:

没有答案