Geting SelectList to MVC view using AJAX/jQuery Geting SelectList to MVC view using AJAX/jQuery ajax ajax

Geting SelectList to MVC view using AJAX/jQuery


Supposing you have a controller action that will feed the data for the dropdown:

public ActionResult Cars(){    return Json(new[] {        new { id = "bmw", name = "BMW" },        new { id = "mer", name = "Mercedes" },        new { id = "aud", name = "Audi" }    }, JsonRequestBehavior.AllowGet);}

And in your view:

$.getJSON('/home/cars', { }, function(cars) {    var list = $('select#cars');    list.find('option').remove();    $(cars).each(function(index, car) {        list.append('<option value="' + car.id + '">' + car.name + '</option>');    });});