I am having Problem with the AJAX and Jquery Response for the DropDown Cascading. I am trying to Change the State as per the Selection of Country list from the dropdown. I think I am having some problem with my Script.
View:
@using (Html.BeginForm("Submit", "FeedBack", FormMethod.Post)){ @Html.ValidationSummary(true) @Html.AntiForgeryToken() <div class="editor-label"> @Html.LabelFor(model => model.countryid, "Country") </div> <div class="editor-field"> @Html.DropDownListFor(model => model.countryid, @ViewBag.CountryID as SelectList, "Select Country") @Html.ValidationMessageFor(model => model.countryid) </div> <div class="editor-label"> @Html.LabelFor(model => model.stateid, "State") </div> <div class="editor-field"> @Html.DropDownListFor(model => model.stateid, @ViewBag.StateID as SelectList, "Select State") @Html.ValidationMessageFor(model => model.stateid) </div> <input type="submit" value="Create" /> } @Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/jqueryval") <script type="javascript"> $(document).ready(function () { $("#countryid").change(function () { // this will call when Country Dropdown select change var countryid = parseInt($("#cointryid").val()); if (!isNaN(countryid)) { var ddState = $("#stateid"); ddState.empty(); // this line is for clear all items from State dropdown ddState.append($("<option></option").val("").html("Select State")); // Here I will call Controller Action via Jquery to load State for selected Country $.ajax({ url: "@Url.Action("GetStates", "Feedback")", type: "GET", data: { countryid: countryid }, dataType: "json", success: function (data) { $.each(data, function (i, item) { ddState.append( $("<option></option>").val(item.stateid).html(item.statename) ); }); }, error: function () { alert("Error!"); } }); } }); });
Controller:
public class FeedBackController : Controller { CountryStateDB db= new CountryStateDB(); public ActionResult Submit() { List<Country> allCountries = new List<Country>(); List<State> allStates = new List<State>(); allCountries = db.Countries.OrderBy(a => a.countryname).ToList(); ViewBag.CountryID = new SelectList(allCountries, "countryid", "countryname"); ViewBag.StateID = new SelectList(allStates, "stateid", "statename"); return View(); } [HttpPost] [ValidateAntiForgeryToken]//// this is for prevent CSRF Attack(cross site Request Attack) public ActionResult Submit(FeedBack fb) { List<Country> allCountry = new List<Country>(); List<State> allState = new List<State>(); allCountry = db.Countries.OrderBy(a => a.countryname).ToList(); if (fb != null && fb.countryid > 0) { allState = db.States.Where(a => a.countryid.Equals(fb.countryid)).OrderBy(a => a.statename).ToList(); } ViewBag.CountryID = new SelectList(allCountry, "countryid", "countryname", fb.countryid); ViewBag.StateID = new SelectList(allState, "stateid", "statename", fb.stateid); if (ModelState.IsValid) { db.FeedBacks.Add(fb); db.SaveChanges(); ModelState.Clear(); fb = null; ViewBag.Message = "Successfully Submitted"; } else { ViewBag.Message = "Failed! Please try again"; } return View(fb); } [HttpGet] public JsonResult GetStates(string countryid = "") { List<State> allstate= new List<State>(); int id = 0; if (int.TryParse(countryid, out id)) { allstate = db.States.Where(a => a.countryid.Equals(id)).OrderBy(a => a.statename).ToList(); } if (Request.IsAjaxRequest()) { return new JsonResult { Data = allstate, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } else { return new JsonResult { Data = "Not Valid Request", JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } } }