ASP.Net MVC 6级联下拉列表没有填充

时间:2016-09-01 07:52:49

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

我在页面上使用级联下拉菜单。第一个下拉列表正在从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方法执行并返回数据,但由于某种原因,这些值不会附加到第二个下拉列表中。

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

1 个答案:

答案 0 :(得分:0)

@StephenMuecke你救了我的同伴!删除selected-select类加载数据。我发现我必须调用

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

更新列表框。非常感谢你指点我的方向:)

相关问题