Hi I am using mvc with jQuery for Dynamic DropDownList
@Html.DropDownList("CountryId", ViewBag.CountryId as SelectList,"Please Select", new { onchange = "javascript:OnCountryChange(this.value);" })
@Html.DropDownList("CityId")
function OnCountryChange(value)
{
$.ajax(
{
type: "POST",
datatype: "json",
url: "/Location/GetCityListByCountryId",
data: "cid=" + value,
ContentType: "application/Json",
success: function (data)
{
var ddl = $("#CityId");
ddl.empty();
if (data.length > 0)
{
$(ddl).append("<option value=''> Please Select</option>");
$.each(data, function (index, optionData)
{
$(ddl).append("<option value='" + optionData.CityId + "'>" + optionData.Name + "</option>");
});
}
}
});
}
public JsonResult GetCityListByCountryId(string cid)
{
int cId = Convert.ToInt32(cid);
JsonResult result = new JsonResult();
result.Data = (from c in db.Cities
where c.CountryId == cId
select new { CityId = c.CityId, Name = c.Name, }).ToList();
result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
return result;
}