javascript - Asp.net MVC 中的三个表级联下拉列表
问题描述
我想在 Asp.Net MVC 中有一个级联下拉列表。我已经设法用两张表做到了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>")
});
});
})
});
解决方案
好吧,只需添加以下内容:
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>")
});
});
})
});
推荐阅读
- python - 使用 & 运算符对 DataFrame 应用两个以上的过滤器
- node.js - 为什么我有 404 错误(Angular 2 和 Node js)
- ruby-on-rails - Rails 5资产不编译图像或css
- typescript - 键入泛型推断类型的数组
- c++ - 为什么不优化常数有符号整数分数的乘法?
- ubuntu - ubuntu xenial/Release' 不再有 Release 文件。ubuntu中的错误
- sql - postgres12 更改表与类型转换语法错误
- django - form_invalid 方法不显示错误的字段
- javascript - 将元素推送到 Javascript (Socket.io) 中的嵌套对象
- javascript - 如何将 HTML 脚本标签复制到外部 javascript?