how to populate a 3rd dropdown list in MVC, using AJAX call and on the basis of the selected value's of second drop down as well
?
Example Country--State--City--Town--Village
how to populate a 3rd dropdown list in MVC, using AJAX call and on the basis of the selected value's of second drop down as well
?
Example Country--State--City--Town--Village
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sachin SinghRank #66 · 26.2K reputationPosted Sep 17, 2022, 4:40 AM EDT
select country select state select city select Townstep 2.
// all binding functions function BindCountry() { $.Post("/Home/GetAllCountries",function(data) { $.each(data,function(x,y){ $("#ddlCountry").append($("",{value:x.Id,text:x.Name})); }); }); } function BindState(countryId) { $.Post("/Home/GetStateByCountry?countryId="+countryId,function(data) { $.each(data,function(x,y){ $("#ddlState").append($("",{value:x.Id,text:x.Name})); }); }); } function BindCity(stateId) { $.Post("/Home/GetCityByState?stateId="+stateId,function(data) { $.each(data,function(x,y){ $("#ddlCity").append($("",{value:x.Id,text:x.Name})); }); }); } function BindTown(cityId) { $.Post("/Home/GetTownByCity?cityId="+cityId,function(data) { $.each(data,function(x,y){ $("#ddlTown").append($("",{value:x.Id,text:x.Name})); }); }); } //ready function and Change event functions of respective ddl $(function(){ BindCountry(); // populate country at page load }); $("#ddlCountry").change(function(){ var countryId= $(this).val(); $("#ddlState").empty(); $("#ddlCity").empty(); $("#ddlTown").empty(); BindState(countryId); //populate state when country changes and empty child ddls }); $("#ddlState").change(function(){ var stateId= $(this).val(); $("#ddlCity").empty(); $("#ddlTown").empty(); BindCity(stateId); //populate city when state changes and empty child ddl ; }); $("#ddlCity").change(function(){ var cityId= $(this).val(); $("#ddlTown").empty(); BindTown(cityId); //populate Town when city changes });MVC methods
public ActionResult GetAllCountries() { // return list of countries var list= _context.Countries.ToList(); return Json(list,JsonRequestBehavior.AllowGet); } public ActionResult GetStateByCountry(int countryId) { // return list of countries var list= _context.States.Where(x=>x.CountryId==countryId).ToList(); return Json(list,JsonRequestBehavior.AllowGet); } public ActionResult GetCityByState(int stateId) { // return list of countries var list= _context.Cities.Where(x=>x.StateId==stateId).ToList(); return Json(list,JsonRequestBehavior.AllowGet); } public ActionResult GetTownByCity(int cityId) { // return list of countries var list= _context.Towns.where(x=>x.CityId==cityId).ToList(); return Json(list,JsonRequestBehavior.AllowGet); }Vishal JoshiRank #377 · 6.7K reputationPosted Sep 17, 2022, 6:38 AM EDT
Hello R D,
The functionality you are looking for is known as cascading dropdown.
You can check below article for the same:
https://www.c-sharpcorner.com/UploadFile/4d9083/creating-simple-cascading-dropdownlist-in-mvc-4-using-razor/
Jignesh KumarRank #91 · 22.9K reputationPosted Sep 17, 2022, 7:21 AM EDT
Hi,
Please find below link which will help you,
https://www.c-sharpcorner.com/Forums/ajax-calling-for-more-than-3-dropdowns-mvc
https://www.aspsnippets.com/Articles/AJAX-Cascading-DropDown-Example-in-ASPNet.aspx
Vishal YelveRank #178 · 13.6K reputationPosted Sep 18, 2022, 1:28 PM EDT
https://www.aspsnippets.com/Articles/Cascading-Dependent-DropDownList-in-ASPNet-MVC.aspx
https://learn.microsoft.com/en-us/answers/questions/216632/cascading-dependent-dropdownlist-using-aspnet-mvc.html