我正在尝试:例如,当从第一个下拉列表中选择“起亚品牌”时,我只想在第二个下拉列表中查看“ kia型号”(而不是查看所有汽车型号),但是它不起作用。这是cshtml中的代码:
@model Tuple< Dalili_2018.Models.Car_Model ,Dalili_2018.Models.Car_Brand >
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_EmployeeLayout.cshtml";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Car</h4>
<hr />
<div class="form-group">
@if (ViewBag.CarBrandList != null)
{
@Html.DropDownListFor(Model => Model.Item2.ID, ViewBag.CarModelList
as SelectList, "---------", htmlAttributes: new { @class = "form-control" })
}
</div>
<div class="form-group">
@Html.DropDownListFor(Model => Model.Item1.ID, new SelectList(" "),
"---------", htmlAttributes: new { @class = "form-control" })
</div>
}
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>
$(document).ready(function () {
$("#Item2.ID").change(function () {
$.get("/CarsController/GetModelCar", { ID_car_brand:
$("#Item2.ID").val() }, function (data) {
$("#item1.ID").empty();
$.each(data, function (Create, row) {
$("#item1.ID").append("<option value='" + row.ID
+ "'>" + row.ModelName + "</option>")
});
});
})
});
</script>
控制器代码为:
public ActionResult Create()
{
List<Car_Model> CarBrandList = db.Car_Brand.ToList();
ViewBag.CarBrandList = new SelectList(CarBrandList, "ID",
"BrandName");
}
public JsonResult GetModelCar(int ID_car_brand)
{
db.Configuration.ProxyCreationEnabled = false;
List<Car_Model> CarModelList = db.Car_Model.Where(x =>
x.ID_car_brand == ID_car_brand).ToList();
return Json(CarModelList, JsonRequestBehavior.AllowGet);
}