视图
<script type="text/javascript">
function getCities(abbr) {
$.ajax({
url: '@Url.Action("SayaclariGetir", "Enerji")',
data: { abbreviation: abbr },
dataType: "json",
type: "POST",
error: function () {
alert("Error!");
},
success: function (data) {
var items = "";
$.each(data, function (i, item) {
items += "<option value=\"" + item.sno + "\">" + item.seri_no + "</option>";
});
$("#sayaclar").html(items);
}
});
}
$(document).ready(function () {
$("#musteriler").change(function () {
var abbr = $("#musteriler").val();
getCities(abbr);
});
});
</script>
<div>
<table>
<tbody>
<tr>
<td>@Html.DropDownListFor(x => x.musteri_id, new SelectList(Model.musteriler, "sno", "musteri_adi"), "-- Müşteri Seçiniz --", new { id = "musteriler" })
</td>
<td>@Html.DropDownListFor(x => x.sayac_id, new SelectList(Model.sayaclar, "sno", "seri_no"), "-- Sayaç Seçiniz --", new { id = "sayaclar" })
</td>
<td>
<input type="submit" value="Kaydet" />
</td>
</tr>
</tbody>
</table>
</div>
控制者
[HttpPost]
public ActionResult SayaclariGetir(string abbreviation)
{
int musteri_id = Int32.Parse(abbreviation);
IEnumerable<TblSayaclar> _sayaclar = entity.TblSayaclar.Where(x => x.musteri_id == musteri_id);
return new JsonResult
{
Data = new
{
success = true,
sayaclar = _sayaclar
},
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
模型
public class TuketimRaporViewModel
{
public IEnumerable<TblMusteriler> musteriler { get; set; }
public IEnumerable<TblSayaclar> sayaclar { get; set; }
public int musteri_id { get; set; }
public int sayac_id { get; set; }
}
当第一个下拉菜单更改时,我收到警报“错误!”。找不到,为什么会收到警报消息?
编辑
当我写这个
sayaclar = new SelectList(_sayaclar,"sno","seri_no")
而不是sayaclar = _sayaclar
时,没有出现错误,但是这次,第二个dropdownlist值是“ undefined”。谢谢。
最佳答案
我写了这个,它有效:
[HttpPost]
public ActionResult SayaclariGetir(string abbreviation)
{
int musteri_id = Int32.Parse(abbreviation);
var _sayaclar = entity.TblSayaclar.Where(x => x.musteri_id == musteri_id).Select(x => new { sno = x.sno, seri_no = x.seri_no });
return Json(_sayaclar, JsonRequestBehavior.AllowGet);
}
关于asp.net-mvc - ASP.NET MVC中的依赖DropDown?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11610273/