视图
<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
时,没有出现错误,但这次,第二个下拉列表值是“未定义的”。
感谢。
答案 0 :(得分:2)
我写了这个并且有效:
[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);
}
答案 1 :(得分:2)
你可以从这里使用AjaxDropdown:http://awesome.codeplex.com