【发布时间】:2023-03-17 20:50:02
【问题描述】:
我在 MVC 应用程序中有 2 个 DropDowns。尝试根据第一个选择的值(级联)填充第二个.. 由于我对 MVC 比较陌生,这给了我一个小问题.. 这就是我的视图中的外观..
我正在根据第一个 DropDown 中的选择获取数据
这是检索数据的JS代码..
<script type="text/javascript">
$('#AllShips').change(function () {
var selectedShip = $("#AllShips").val();
console.log(selectedShip);
var arrivalSelect = $('#AllShipArrivals');
arrivalSelect.empty();
if (selectedShip != null && selectedShip != '') {
$.getJSON('@Url.Action("GetArrivals")', { ShipID: selectedShip }, function (arrivals) {
console.log(arrivals);
if (arrivals != null && !jQuery.isEmptyObject(arrivals))
{
arrivalSelect.append($('<option/>', {
value: null,
text: ""
}));
$.each(arrivals, function (index, arrival) {
console.log(arrival.Value);
console.log(arrival.Text);
arrivalSelect.append($('<option/>', {
value: arrival.Value,
text: arrival.Text
}));
});
};
});
}
});
这是我的 HTML
<div class="col-lg-2 form-group">
@Html.LabelFor(model => model.AllShips, htmlAttributes: new { @class = "control-label col-md-12" })
<div class="col-md-12">
@if (Model.AllShips != null)
{
@Html.DropDownListFor(model => model.ShipID, Model.AllShips, new { @class = "form-control", id = "AllShips" })
@Html.ValidationMessageFor(model => model.ShipID, "", new { @class = "text-danger" })
}
</div>
</div>
<div class="col-lg-2 form-group">
@Html.LabelFor(model => model.AllShipArrivals, htmlAttributes: new { @class = "control-label col-md-12" })
<div class="col-md-12">
@if (Model.AllShipArrivals != null)
{
@Html.DropDownListFor(model => model.ArrivalId, Model.AllShipArrivals, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ArrivalId, "", new { @class = "text-danger" })
}
else
{ @Html.DropDownListFor(model => model.ArrivalId, null, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ArrivalId, "", new { @class = "text-danger" })}
</div>
</div>
第二个 DropDown 仍然没有改变(最初我用所有到达的列表填充它,是的,我知道..不是一个好的选择)
知道我在这里做错了什么吗?
【问题讨论】:
标签: javascript c# jquery asp.net asp.net-mvc