I need to bind the ddl values from another dropdownlist(ddl) result using ajax and mvc3
Related: how to get the result from controller in ajax json
I need to bind the ddl values from another dropdownlist(ddl) result using ajax and mvc3
Related: how to get the result from controller in ajax json
Better try the below approach for cascading dropdownlist.
[Script]
function SetdropDownData(sender, args) {
$('#ShipCountry').live('change', function () {
$.ajax({
type: 'POST',
url: 'Home/GetCities',
data: { Country: $('#ShipCountry').val() },
dataType: 'json',
success: function (data) {
$('#ShipCity option').remove();
$.each(data, function (index, val) {
var optionTag = $('<option></option>');
$(optionTag).val(val.Value).text(val.Text);
$('#ShipCity').append(optionTag);
});
}
});
});
}
[Controller]
public IEnumerable<SelectListItem> Cities(string Country) // ShipCity is filtered based on the ShipCountry value
{
var Cities = new NorthwindDataContext().Orders.Where(c=>c.ShipCountry == Country).Select(s => s.ShipCity).Distinct().ToList();
List<SelectListItem> type = new List<SelectListItem>();
foreach (var city in Cities)
{
if (city != null)
{
SelectListItem item = new SelectListItem() { Text = city.ToString(), Value = city.ToString() };
type.Add(item);
}
}
return type;
}
public ActionResult GetCities(string Country)
{
return Json(Cities(Country), JsonRequestBehavior.AllowGet);
}