My View is
TypeScript
@model TryMVC.Models.MobileData @{ ViewBag.Title = "SelectDistrict"; } script src="~/Scripts/jquery-1.7.1.min.js">script> h2>Cascading Dropdownlisth2> table> tr> td> label>Statelabel> td> td> @Html.DropDownListFor(x => x.stateNamesModel, Model.stateNamesModel, "--Select--", new { @id="ddlState"}); td> tr> tr> td> label>Districtlabel> td> td id="District"> @Html.DropDownListFor(x => x.DistrictNamesModel, new ListSelectListItem>(), "--Select--", new { @id = "ddlDistrict" }); td> tr> table> script type="text/javascript"> $(document).ready(function () { $('#ddlState').change(function () { $.ajax({ type: "post", url: "/DropDownList/GetDistrict", data: { stateId: $('#ddlState').val() }, datatype: "json", traditional: true, success: function (data) { var district = ""; district = district + '--Select--'; for (var i = 0; i ' + data[i].Value + '>' + data[i].Text + ''; } district = district + ''; $('#District').html(district); } }); }); }); script>My Controller Is
Java
public ActionResult SelectDistrict() { MobileData md = new MobileData(); DBData dd = new DBData(); DataSet ds = new DataSet(); ds = dd.getState(); ListSelectListItem> stateNames = new ListSelectListItem>(); foreach (DataRow item in ds.Tables[0].Rows) { stateNames.Add(new SelectListItem { Text = item["StateName"].ToString() ,Value=(item["StateId"].ToString()) }); } md.stateNamesModel = stateNames; return View(md); } [HttpPost] public ActionResult GetDistrict(string stateId) { MobileData md = new MobileData(); int statId; ListSelectListItem> DistrictNames = new ListSelectListItem>(); if (!string.IsNullOrEmpty(stateId)) { statId = Convert.ToInt32(stateId); DBData dd = new DBData(); DataSet ds = new DataSet(); ds = dd.GetCityOnstate(statId); foreach (DataRow item in ds.Tables[0].Rows) { DistrictNames.Add(new SelectListItem { Text = item["City"].ToString() , Value=(item["CityId"].ToString()) }); } } return Json(DistrictNames, JsonRequestBehavior.AllowGet); }C#
public class MobileData { public IListSelectListItem> stateNamesModel { get; set; } public IListSelectListItem> DistrictNamesModel { get; set; } }My problem is , The dropdownlist for State is populated but [HttpPost] event for GetDistrict is not Firing....Help me plz
Code
GetDistrict
Midhun TpRank #388 · 6.5K reputationPosted Aug 17, 2016, 1:17 AM EDT
Posted Aug 17, 2016, 1:26 AM EDT
@Html.DropDownListFor(x => x.DistrictNamesModel, new List<SelectListItem>(), "--Select--", new { @id = "ddlDistrict" });Posted Aug 17, 2016, 1:34 AM EDT
td> @Html.DropDownListFor(x => x.stateNamesModel, Model.stateNamesModel, "--Select--", new { @id="ddlState"}); td>td id="District"> @Html.DropDownListFor(x => x.DistrictNamesModel, new ListSelectListItem>(), "--Select--", new { @id = "ddlDistrict" }); td>Bikesh SrivastavaRank #108 · 20.3K reputationPosted Aug 17, 2016, 4:53 AM EDT
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts