I am making an application that deals with vehicles. I need two DropDownLists:
This is what I ended up doing... Didn't need additional plugins / 1000 Lines of code...
//The first DDL is being fed from a List in my ViewModel, You can change this...
<%: Html.DropDownList("MakeList", new SelectList(Model.Makes, "ID", "Name")) %>
$(document).ready(function () {
$('#MakeList').change(function () {
$.ajaxSetup({ cache: false });
var selectedItem = $(this).val();
if (selectedItem == "" || selectedItem == 0) {
//Do nothing or hide...?
} else {
$.post('<%: ResolveUrl("~/Sell/GetModelsByMake/")%>' + $("#MakeList > option:selected").attr("value"), function (data) {
var items = "";
$.each(data, function (i, data) {
items += "";
});
$("#ModelID").html(items);
$("#ModelID").removeAttr('disabled');
});
}
});
});
[HttpPost]
public ActionResult GetModelsByMake(int id)
{
Models.TheDataContext db = new Models.TheDataContext();
List models = db.Models.Where(p=>p.MakeID == id).ToList();
return Json(models);
}