gpt4 book ai didi

asp.net-mvc - ASP.NET MVC 中的依赖下拉菜单?

转载 作者:行者123 更新时间:2023-12-01 03:19:26 24 4
gpt4 key购买 nike

查看

<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>

Controller

[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 时,没有发生错误,但是,这一次,第二次下拉列表值“未定义”。

谢谢。

最佳答案

我写了这个并且它有效:

[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 中的依赖下拉菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11610273/

24 4 0