This question is locked. New answers and comments are not allowed.
Hi All,
I have just started using telerik mvc extensions and I noticed a strange behavior when I tried to cascade three DropDownList. Basically I have a DropDownLists for "State", "City" and "Locality". The change in State should cascade to City and Locality DropDownLists. For example when I select California as state, I should get list of all cities in California in the City DropDownList and Locality DropDownList should be updated to list all Localities for first City in California.
With the code that I have built up, the City DropDownList is getting properly updated when the State dropdown changes, but the Locality DropDownList still displays the old values. It gets updated to display new values only after I click on the Locality list to select a value.
Here is the code that I am using. Why does the LocalityDropDown list require a click to render new values ?
I have just started using telerik mvc extensions and I noticed a strange behavior when I tried to cascade three DropDownList. Basically I have a DropDownLists for "State", "City" and "Locality". The change in State should cascade to City and Locality DropDownLists. For example when I select California as state, I should get list of all cities in California in the City DropDownList and Locality DropDownList should be updated to list all Localities for first City in California.
With the code that I have built up, the City DropDownList is getting properly updated when the State dropdown changes, but the Locality DropDownList still displays the old values. It gets updated to display new values only after I click on the Locality list to select a value.
Here is the code that I am using. Why does the LocalityDropDown list require a click to render new values ?
<!-- cshtml -->@(Html.Telerik().DropDownListFor(m => m.SelectedStateId) .Name("SelectedStateId") .BindTo(new SelectList(Model.States, "Id", "Name")) .ClientEvents(events => events.OnChange("stateChanged")))<p />@(Html.Telerik().DropDownListFor(m => m.SelectedCityId) .Name("SelectedCityId") .BindTo(new SelectList(Model.Cities, "Id", "Name")) .ClientEvents(events => events.OnChange("cityChanged")))<p />@(Html.Telerik().DropDownListFor(m => m.SelectedLocalityId) .Name("SelectedLocalityId") .BindTo(new SelectList(Model.Localities, "Id", "Name")))<p /> <input type="submit" value="Submit" />} <script type="text/javascript"> function stateChanged(e) { var url = '@Url.Action("_PopulateCities", "Home")'; var SelectedCityId = $('#SelectedCityId').data('tDropDownList'); SelectedCityId.loader.showBusy(); $.get(url, { SelectedStateId: e.value }, function (data) { SelectedCityId.dataBind(data); SelectedCityId.loader.hideBusy(); SelectedCityId.select(0); }); } function cityChanged(e) { var url = '@Url.Action("_PopulateLocalities", "Home")'; var SelectedLocalityId = $('#SelectedLocalityId').data('tDropDownList'); SelectedLocalityId.loader.showBusy(); $.get(url, { SelectedCityId: e.value }, function (data) { SelectedLocalityId.dataBind(data); SelectedLocalityId.loader.hideBusy(); SelectedLocalityId.select(0); }); } </script>public JsonResult _PopulateCities(int SelectedStateId) { using (var context = new LocationContext()) { List<City> cities = context.Cities.Where(c => c.State.Id == SelectedStateId).ToList(); return Json(new SelectList(cities,"Id","Name"), JsonRequestBehavior.AllowGet); }}public JsonResult _PopulateLocalities(int SelectedCityId) { using (var context = new LocationContext()) { List<Locality> localities = context.Localities.Where(l => l.City.Id == SelectedCityId).ToList(); return Json(new SelectList(localities, "Id", "Name"), JsonRequestBehavior.AllowGet); }}