MVC/JSON级联下拉列表显示[对象对象]

本文关键字:对象 列表显示 JSON 级联 MVC | 更新日期: 2023-09-27 18:00:42

来自相对新手的问候。

我正试图根据另一个下拉列表(代理商)中的选定值填充下拉列表(收单地址)。当在第一个下拉列表中选择值时,从属下拉列表显示[对象对象]。我需要第二个下拉列表中的ID来更新数据库记录。

我相信我在这里错过了一些基本/愚蠢的东西,但我几乎放弃了。我在这里搜索了类似的问题,但还没有找到任何解决我问题的方法。

非常感谢!

以下是相关的代码片段:

CreatePurchaseOrderViewModel.cs

// Agency dropdown list
[Required(ErrorMessage = "An agency must be selected.")]
public int AgencyID { get; set; }
public List<SelectListItem> AgencyList { get; set; }
// Agency bill-to address dropdown list
[Required(ErrorMessage = "A bill-to address must be selected.")]
public int BillToAddressID { get; set; }
public List<SelectListItem> BillToAddressList { get; set; }

PurchaseOrderService.cs

// Query vPurchaseOrderAddresses view to fetch
// a bill-to address for a given Id
public List<SelectListItem> GetBillToAddressByAgency(int Id)
{
    using (MyEntities db = new MyEntities())
    {
        var billtoAddressQuery = db.vPurchaseOrderAddresses.ToList()
            .Where(b => b.PartyID == Id)
            .Where(b => b.PCMPT == 2) // (2) = bill-to address
            .Select(b => new SelectListItem
            {
                Value = b.PartyID.ToString(),
                Text = b.Address
            });
        // Sort list ascending
        billtoAddressQuery = billtoAddressQuery.OrderBy(d => d.Text);
        return billtoAddressQuery.ToList();
    }
}

PurchaseOrderController.cs

[HttpPost]
public ActionResult GetBillToAddressByAgency(string selectedValue)
{
    // Create instance of PurchaseOrderService class
    PurchaseOrderService PO = new PurchaseOrderService();
    // Create a list to hold bill-to addresses from database
    List<SelectListItem> BillToAddressList = new List<SelectListItem>();
    // Populate bill-to address dropdown list by calling
    // the GetBillToAddressByAgency method of the PurchaseOrderService class.
    int i;
    if (Int32.TryParse(selectedValue, out i))
    {
        BillToAddressList = PO.GetBillToAddressByAgency(Convert.ToInt32(selectedValue));
    }
    return Json(new { billtoAddressList = BillToAddressList });
}

最后,视图:Create.cshtml

@*-- Agency dropdown list --*@
<div class="form-group">
    <label class="control-label col-md-2" for="AgencyID">Agency *</label>
    <div class="col-md-10">
        @Html.DropDownListFor(model => model.AgencyID, Model.AgencyList, "-- Select Agency --")
        @Html.ValidationMessageFor(model => model.AgencyID)   
    </div>
</div>
<div class="form-group">
    <label class="control-label col-md-2" for="BillToAddressID">Bill-To Address *</label>
    <div class="col-md-10">
        @Html.DropDownListFor(model => model.BillToAddressID, Model.BillToAddressList, "-- Select Bill-To Address --")
        @Html.ValidationMessageFor(model => model.BillToAddressID)
    </div>
</div>
<script type="text/javascript">
    $("#AgencyID").change(function () {
        var selectedValue = $(this).val();
        $.ajax({
            url: '@Url.Action("GetBillToAddressByAgency", "PurchaseOrder")',
            type: 'POST',
            data: { "selectedValue": selectedValue },
            dataType: 'json',
            success: function (response) {
                var items = "";
                $.each(response.billtoAddressList, function (i, item) {
                    items += "<option value='"" + item + "'">" + item + "</option>";
                });
                $("#BillToAddressID").html(items);
            },
            error: function (error) {
            }
        });
    });
</script>

MVC/JSON级联下拉列表显示[对象对象]

替换此行items += "<option value='"" + item + "'">" + item + "</option>";

通过items += "<option value='"" + item.Value + "'">" + item.Text + "</option>";

关于您的代码。因为您可以检索对象,这是好的,但您需要访问属性。我希望它能帮助你。