ASP.NET MVC Core Cascading DropDownList

前端 未结 1 1316
死守一世寂寞
死守一世寂寞 2021-01-19 01:13

I\'m having trouble finding a tutorial / video that shows how to implement Cascading DropDownList from a Database using EntityFramework. I\'m using ASP.NET MVC Core, EntityF

1条回答
  •  生来不讨喜
    2021-01-19 01:26

    I had a similar situation but in my example I have a Root folder and depending on which root folder I am using the next drop down list would display the corresponding sub-folders.

    Not sure if there is a purly asp.net solution but, I used Jquery/Ajax for this.

    Your code should look something like this:

    html list:

    
    
    
    
    
    
    
    

    Jquery code, you write this in .js file and then add it to a specific view with this statement, Don't forget you need to add a jquery library to your project before any other javascript, and your example.js will contain:

    $(document).ready(function () {
      $("#state-target").on("change", function () {
        $list = $("#city-target");
        $.ajax({
            url: "/getCities",
            type: "GET",
            data: { id: $("#state-target").val() }, //id of the state which is used to extract cities
            traditional: true,
            success: function (result) {
                $list.empty();
                $.each(result, function (i, item) {
                    $list.append('');
                });
            },
            error: function () {
                alert("Something went wrong call the police");
            }
        });
      });
    });
    

    The Ajax request will call this action in the Controller which will retrieve a list of cities from the database (using something like return dbContext.CityTable.Where(c => c.StateId == id).ToList() inside a getCititesFromDatabaseByStateId(id) method) and then return the Json object, the success function will create a list of options and apply it:

    public IActionResult getCities(int id)
    {
        var cities = new List();
        cities = getCititesFromDatabaseByStateId(id); //call repository
        return Json(citites);
    }
    

    In your ViewModel consider changing IEnumerable (IEnumerable) to IEnumerable. I can say as well your Model's are messy (but if you can get data the from the database focus on getting the list working 1st), consider improving them later.

    Fix for 2 errors mentioned in the comments:

    public List getCititesFromDatabaseByStateId(int id)
    {
        return db.Citys.Where(c => c.StateId == id).ToList();
    }
    
    public ActionResult Create()
    {
         var states = new SelectList(db.States.ToList(), "StateId", "Abbr");
         var citys = new SelectList(db.Citys.ToList(), "CityId", "Name");
         var zips = new SelectList(db.Zips.ToList(), "ZipId", "Code");
    
         var viewModel = new CustomerFormVM
         {
             States = states,
             Citys = citys,
             Zips = zips
         };
    
         return View(viewModel);
    }
    

    0 讨论(0)
提交回复
热议问题