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

时间:2014-04-29 17:57:25

标签: c# jquery json asp.net-mvc-5 entity-framework-6

来自相对新手的问候。

我正在尝试根据另一个下拉列表(代理商)中的选定值填充下拉列表(收单地址)。在第一个下拉列表中选择值时,从属下拉列表显示[对象对象]。我需要第二个下拉列表中的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>

1 个答案:

答案 0 :(得分:0)

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

items += "<option value=\"" + item.Value + "\">" + item.Text + "</option>";

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