【问题标题】:Three Tables Cascading DropdownList in Asp.net MVCAsp.net MVC 中的三个表级联下拉列表
【发布时间】:2018-09-21 12:34:30
【问题描述】:

我想在 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>")
            });
        });
    })
});

【问题讨论】:

  • 那么你为城市尝试了什么?从州到市的变化不是很小吗?

标签: javascript c# json asp.net-mvc


【解决方案1】:

好吧,添加这个:

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

【讨论】:

    猜你喜欢
    • 2011-07-05
    • 2023-03-30
    • 2021-03-04
    • 2014-05-22
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    • 1970-01-01
    • 2011-10-05
    相关资源
    最近更新 更多