This is what I ended up doing... Didn't need additional plugins / 1000 Lines of code...
The Html
//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")) %>
<select id="ModelID" name="ModelID" disabled="disabled"></select>
The JQuery
$(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 += "<option value='" + data.ID + "'>" + data.Name + "</option>";
});
$("#ModelID").html(items);
$("#ModelID").removeAttr('disabled');
});
}
});
});
The Action
[HttpPost]
public ActionResult GetModelsByMake(int id)
{
Models.TheDataContext db = new Models.TheDataContext();
List<Models.Model> models = db.Models.Where(p=>p.MakeID == id).ToList();
return Json(models);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…