简体   繁体   中英

How to refresh new data of second drop down list related to my first drop down list in mvc

I have two dropdownlists. The first one is country and the second one is province. Data for the both are loaded from the database. My Country table are consist of only 2 fields, CountryID and CountryName and the Province table has 3 fields CountryID,ProvinceID,ProvinceName. I want to refresh my province dropdownlist when I select my country dropdown list and here is my code:

        <div class="form-group">
            @Html.LabelFor(model => model.Country, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @{
                    TBSWebApp.Database_Access_Layer.db dblayer2 = new TBSWebApp.Database_Access_Layer.db();
                    ViewBag.category = new SelectList(dblayer2.GetCountry(), "Country1", "Country");
                }
                @Html.DropDownListFor(model => model.Country ,
                (IEnumerable<SelectListItem>)ViewBag.category,

                new { @class = "form-control" })

                @Html.ValidationMessageFor(model => model.Country, "", new { @class = "text-danger" })
            </div>
        </div>

and this one is for calling my database and fill the record

    public IEnumerable<TBSWebApp.Models.ContactAddress> GetCountry()
    {

        SqlCommand com = new SqlCommand("SELECT CountryID,CountryName FROM tblCountry ORDER BY Country", sqlconn);
        SqlDataAdapter da = new SqlDataAdapter(com);
        DataSet ds = new DataSet();
        da.Fill(ds);
        var tankReadings = new List<TBSWebApp.Models.ContactAddress>();
        int x = 0;
        foreach (DataRow drow in ds.Tables[0].Rows)
        {
            var tankReading = new TBSWebApp.Models.ContactAddress
            {
                Country1 = drow["CountryID"].ToString(),
                Country = drow["CountryName"].ToString()

            };

            tankReadings.Add(tankReading);
        }
        return tankReadings.AsEnumerable();
    }

    public IEnumerable<TBSWebApp.Models.ContactAddress> GetProvince(string ID)
    {

        SqlCommand com = new SqlCommand("SELECT ProvinceID,ProvinceName FROM tblProvince WHERE CountryID ='" + ID + "' ORDER BY Province", sqlconn);
        SqlDataAdapter da = new SqlDataAdapter(com);
        DataSet ds = new DataSet();
        da.Fill(ds);
        var tankReadings = new List<TBSWebApp.Models.ContactAddress>();
        int x = 0;
        foreach (DataRow drow in ds.Tables[0].Rows)
        {
            var tankReading = new TBSWebApp.Models.ContactAddress
            {
                Province1 = drow["ProvinceID"].ToString(),
                Province = drow["ProvinceName"].ToString()

            };

            tankReadings.Add(tankReading);
        }
        return tankReadings.AsEnumerable();
    }

and now I don't know how to call my province dropdownlist to fill the records based on selected country.

You can use javascript to catch event change of select tag and send data option selected via ajax for getting new Providers list.

var selectDom = document.getElementById(\*id_your_select_tag*\);
selectDom.addEventListener("change", function() {
    // call ajax at here 
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
           // xhttp.responseText is response provider list.
           // update select provider at here
        }
    }
    xhttp.open("GET", "your_url_api_get_provider_list_by_id", true);
    xhttp.send();

One other way, I can use partial view to show provider list. See here Update Partial View with Ajax

Your requirement is known as Drop down cascading in MVC. I am sharing a very similar articles to your requirement. just go through, Article have StateMaster which is correspond to My country table and DistrictMaster which is correspond to Province table.

https://www.c-sharpcorner.com/UploadFile/sourabh_mishra1/cascading-dropdownlist-in-Asp-Net-mvc/

Please check and let me know if you need more help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM