In fact, I am filling out a dropdown list of states in the json country selection. I like it
I have an action in my controller, it returns my data in json format, as below
public JsonResult State(int countryId) { var stateList = CityRepository.GetList(countryId); return Json(stateList, JsonRequestBehavior.AllowGet); }
in my opinion
<script type="text/javascript"> function cascadingdropdown() { $("#stateID").empty(); $("#stateID").append("<option value='0'>--Select State--</option>"); var countryID = $('#countryID').val(); $.ajax({ url: "/City/State", dataType: 'json', data: { countryId: countryID }, success: function (data) { $("#stateID").empty(); $("#stateID").append("<option value='0'>--Select State--</option>"); $.each(data, function (index, optiondata) { alert(optiondata.StateName); $("#stateID").append("<option value='" + optiondata.ID + "'>" + optiondata.StateName + "</option>"); }); }, error: function () { alert('Faild To Retrieve states.'); } }); } </script>
I think this will help you ...
source share