【问题标题】:ASP.Net MVC 6 Cascading Dropdowns Not Populating [duplicate]ASP.Net MVC 6级联下拉菜单不填充[重复]
【发布时间】:2016-09-01 07:52:48
【问题描述】:

我在页面上使用级联下拉菜单。第一个下拉列表是从 ViewBag 中填充的

@Html.DropDownList("OrgGroupID",
                                        (IEnumerable<SelectListItem>)ViewBag.OrgGroups,
                                        new Dictionary<string, object>
                                        {
                                            { "class", "form-control" },
                                            { "width", "100%" },
                                            { "data-placeholder", "Select Group..." },
                                            { "onchange", "groupSelected(this)" }
                                        })

当从第一个值中选择一个值时,第二个值被填充。第二个下拉菜单的标记是

@Html.ListBox("Devices",
                            (IEnumerable<SelectListItem>)ViewBag.Devices, 
                                new Dictionary<string, object>
                                {
                                    { "id", "devices"},
                                    { "class", "chosen-select form-control" },
                                    { "width", "100%" },
                                    { "data-placeholder", "Select Devices..." }
                                })  

填充第二个下拉列表的jquery函数是

function groupSelected(obj) {            
var selectedGroupId = obj.options[obj.selectedIndex].value;            

$.ajax({
    url: "../Devices/DevicesByGroupId",
    type: "GET",
    dataType: "JSON",
    data: { groupId: selectedGroupId },
    success: function (devicesData) {                                             
        $("#devices").html("");
        $.each(devicesData, function (index, itemData) {                        
            $("#devices").append(
                $("<option></option>").val(itemData.Value).html(itemData.Text)
            );                        
        });
    }
});
}

正在调用的 api 方法执行并返回数据,但由于某种原因,这些值未附加到第二个下拉列表中。

请建议我在这里缺少什么。

【问题讨论】:

  • 你的类名建议你使用选择的插件。暂时禁用它并确认正在加载选项

标签: asp.net-mvc listbox jquery-chosen dropdown cascadingdropdown


【解决方案1】:

@StephenMuecke 你救了我的好朋友!删除选择的选择类加载数据。我刚刚发现我必须调用

$("#devices").trigger("chosen:updated");

更新列表框。非常感谢您指出我这个方向:)

【讨论】:

    猜你喜欢
    • 2016-10-27
    • 2018-04-25
    • 1970-01-01
    • 1970-01-01
    • 2011-06-23
    • 2022-09-26
    • 1970-01-01
    • 1970-01-01
    • 2013-08-23
    相关资源
    最近更新 更多