Used Nimith Joshi's article : Working With DropDownList in MVC 5 Using jQuery
Issue : Not displaying "State".
Model.Countries : I could populate dropdownlist for "Country" from the database.
public JsonResult States(string Country)
{
List
switch (Country) {
case "Canada":
statesList.Add("Quebec");
statesList.Add("Ontario");
break;
case "Australia":
statesList.Add("Melbourne");
statesList.Add("Sydney");
break;
case "UNITED STATES":
statesList.Add("New York");
statesList.Add("Washington");
break; }
return Json(statesList);
}
cshtml :
@Html.DropDownListFor(model => model.Country, new SelectList(Model.Countries, "Key", "Value"))
Thanks in advance!
Nimit JoshiPosted Feb 17, 2014, 12:23 AM
<script src="~/Scripts/jquery-1.10.2.js">script>
<script>
$(document).ready(function () {
$("#State").prop("disabled", true);
$("#Country").change(function () {
if ($("#Country").val() != "Select") {
var CountryOptions = {};
CountryOptions.url = "/Sample/states";
CountryOptions.type = "POST";
CountryOptions.data = JSON.stringify({ Country: $("#Country").val() });
CountryOptions.datatype = "json";
CountryOptions.contentType = "application/json";
CountryOptions.success = function (StatesList) {
$("#State").empty();
for (var i = 0; i < StatesList.length; i++) {
$("#State").append(" + StatesList[i] + "");
}
$("#State").prop("disabled", false);
};
CountryOptions.error = function () { alert("Error in Getting States!!"); };
$.ajax(CountryOptions);
}
else {
$("#State").empty();
$("#State").prop("disabled", true);
}
});
});
script>
Vijay SaklaniPosted Feb 21, 2014, 1:40 AM
check this link:
populate dropdownlist