CaseCading Drop down in MVC4 or 5


 First Create the MVC Application. Create a controller for the Case Cading drop down. 
 For retrieving the data From the Data Base You Have to Create a Entity Model First.
 And then add the Bellow Action Methods in Controller
       
        TestDBEntities objEntity = new TestDBEntities();
        public ActionResult Index()
        {

            ViewBag.Country = new SelectList(GetAllcountry(), "CountryId""CountryName");
            return View();
        }
       
        This method for the Get All country return in the List

        public List<Country> GetAllcountry()
        {
            var data = (from val in objEntity.Countries select val).ToList();

            return data;
        }
        
        This method for the Get All States by Country Id return in data in JSON
        public JsonResult GetAllStates(int Id)
        {
            var data = (from val in objEntity.States
                        where val.CountryId == Id
                        select val).ToList();
            return Json(data, JsonRequestBehavior.AllowGet);

        }
        
       This method for the Get All City by State Id return in data in JSON
        public JsonResult GetAllCity(int id)
        {
            var data = (from val in objEntity.CityMasters
                        where val.StateId == id
                        select val).ToList();
            return Json(data, JsonRequestBehavior.AllowGet);
        }

  After you have to create a View For Index Action Method.

 <script src="~/Scripts/jquery-1.10.2.js"></script>
 <script >

     $(function () {
        // Drop down Change For Getting the Country Wise State
        $('#Country').change(function () {
            var countryId = $('#Country').val();          
               
 // Getting Data Form the Controller method in JSON           
 $.getJSON('/CaseCadingTest/GetAllStates/' + countryId, function (data) {
                
                var items = '<option>Select State</option>';
                $.each(data, function (i, item) {
                    items += "<option  value='" + item.StateId + "'>" + item.StateName + "</option>";
                });

           
                $('#states').html(items);
            });
        });
   
        // Drop down Change For Getting the state Wise city
          $('#states').change(function () {
            var stateid = $('#states').val();
            $.getJSON('/CaseCadingTest/GetAllCity/' + stateid, function (data) {
                var items = '<option>Select City</option>';
                $.each(data, function (i, item) {
                    items += "<option value='" + item.CityID + "'>" + item.CityName + "</option>"
                });
                $('#City').html(items);
            });
        });
     });
</script>
@using (Html.BeginForm())
{
    @Html.DropDownList("Country",ViewBag.Country as SelectList)

    <select id="states"  name="states"></select>
    <select id="City" name="City"></select>
}


Comments

Popular posts from this blog

Validate Mobile Number with 10 Digits in ASP.Net