我有兩個表一個製造商表和ManufacturerModel表。我正在嘗試填充兩個下拉列表。製造商名單,然後根據選擇哪個製造商,它會列出模型列表。但是我實現的不起作用。它什麼都不做。MVC級聯式下拉
我創建結合了兩種模式
public class ManufacturerModelDD
{
public DbSet<Manufacturer> Manufacturers { get; set; }
public DbSet<ManufacturerModel> ManufacturerModels { get; set; }
}
一個新視圖模型,我已經在我希望他們在控制器中創建2種功能。
ManufacturerModelDD mm = new ManufacturerModelDD();
public JsonResult GetManufacturers()
{
return Json(mm.Manufacturers.ToList(), JsonRequestBehavior.AllowGet);
}
public JsonResult GetModelsByManufacturerID(string manufacuterId)
{
int Id = Convert.ToInt32(manufacuterId);
var models = from a in mm.ManufacturerModels where a.ManufacturerID == Id select a;
return Json(models);
}
在我看來,我有
<script>
$(function(){
$.ajax({
type: "GET",
url: "/Device/GetManufacturers",
datatype: "Json",
success: function (data) {
$.each(data, function (index, value) {
$('#dropdownManufacturer').append('<option value="' + value.ManufacturerID + '">' +
value.Manufacturer1 +'</option>');
});
}
});
$('#dropdownManufacturer').change(function(){
$('#dropdownModel').empty();
$.ajax({
type: "POST",
url: "/Device/GetModelsByManufacturerID",
datatype: "Json",
data: { manufacturerID: $('#dropdownManufacturer').val() },
success: function (data) {
$.each(data, function (index, value) {
$('#dropdownModel').append('<option value="' + value.ManufacturerID + '">' +
value.Model + '</option>');
});
}
});
});
});
哪一部分是不工作的看法特定的JavaScript代碼? – Shyju
我不確定。我的下拉菜單是空白的。我不確定它是我的函數還是ajax。 –