Javascript Asp.net MVC中的三表级联DropdownList
我想在Asp.NETMVC中有一个级联下拉列表。我已经设法用两个表Javascript Asp.net MVC中的三表级联DropdownList,javascript,c#,json,asp.net-mvc,Javascript,C#,Json,Asp.net Mvc,我想在Asp.NETMVC中有一个级联下拉列表。我已经设法用两个表Country和State,现在我想添加City public class Country { [Key] public int cId { get; set; } public string cName { get; set; } public ICollection<State> state { get; set; } } public class State { [Key
Country
和State
,现在我想添加City
public class Country
{
[Key]
public int cId { get; set; }
public string cName { get; set; }
public ICollection<State> state { get; set; }
}
public class State
{
[Key]
public int sId { get; set; }
public string sname { get; set; }
public int cId { get; set; }
public Country country { get; set; }
}
//Get list of States
public JsonResult GetStateList(int cId)
{
db.Configuration.ProxyCreationEnabled = false;
List<State> listState = db.States.Where(x => x.cId == cId).ToList();
return Json(listState,JsonRequestBehavior.AllowGet);
}
//Script that invokes the Method
$(document).ready(function () {
$("#cId").change(function () {
$.get("/Home/GetStateList", { cId: $("#cId").val() }, function (data) {
$("#sId").empty();
$.each(data, function (index, row) {
$("#sId").append("<option value= '"+row.sId+"'>"+ row.sname+"</option>")
});
});
})
});
公共类国家
{
[关键]
公共int-cId{get;set;}
公共字符串cName{get;set;}
公共ICollection状态{get;set;}
}
公共阶级国家
{
[关键]
公共int sId{get;set;}
公共字符串sname{get;set;}
公共int-cId{get;set;}
公共国家{get;set;}
}
//获取状态列表
公共JsonResult GetStateList(int-cId)
{
db.Configuration.ProxyCreationEnabled=false;
List listState=db.States.Where(x=>x.cId==cId.ToList();
返回Json(listState,JsonRequestBehavior.AllowGet);
}
//调用该方法的脚本
$(文档).ready(函数(){
$(“#cId”)。更改(函数(){
$.get(“/Home/GetStateList”,{cId:$(“#cId”).val()},函数(数据){
$(“#sId”).empty();
$.each(数据、函数(索引、行){
$(“#sId”).append(“+row.sname+”)
});
});
})
});
我们只需添加以下内容:
public class City
{
[Key]
public int cityId { get; set; }
public string cityName { get; set; }
public int sId { get; set; } // stateId
public State state { get; set; }
}
public JsonResult GetCityList(int sId)
{
db.Configuration.ProxyCreationEnabled = false;
List<City> listCity = db.Cities.Where(x => x.sId == sId).ToList();
return Json(listCity,JsonRequestBehavior.AllowGet);
}
$(document).ready(function () {
$("#sId").change(function () {
$.get("/Home/GetCityList", { sId: $("#sId").val() }, function (data) {
$("#cityId").empty();
$.each(data, function (index, row) {
$("#cityId").append("<option value= '"+row.cityId+"'>"+ row.cityName+"</option>")
});
});
})
});
公共级城市
{
[关键]
public int cityId{get;set;}
公共字符串cityName{get;set;}
公共int sId{get;set;}//stateId
公共状态状态{get;set;}
}
公共JsonResult GetCityList(int sId)
{
db.Configuration.ProxyCreationEnabled=false;
List listCity=db.Cities.Where(x=>x.sId==sId.ToList();
返回Json(listCity,JsonRequestBehavior.AllowGet);
}
$(文档).ready(函数(){
$(“#sId”).change(函数(){
$.get(“/Home/GetCityList”,{sId:$(“#sId”).val()},函数(数据){
$(“#cityId”).empty();
$.each(数据、函数(索引、行){
$(“#cityId”).append(“+row.cityName+”)
});
});
})
});
那么,您在城市中做了哪些尝试?从一个州转到另一个城市不是变化很小吗