Три таблицы каскадных DropdownList в Asp.net MVC - PullRequest
0 голосов
/ 21 сентября 2018

Я хочу иметь каскадный выпадающий список в 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>")
            });
        });
    })
});

1 Ответ

0 голосов
/ 21 сентября 2018

Ну, просто добавьте это:

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>")
            });
        });
    })
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...