簡體   English   中英

從ListBox中獲取選定的項目(由Ajax填充)

[英]Get selected items from ListBox (populated with Ajax)

我正在嘗試從多選列表中獲取選定的一個或多個項目。 我真正想要檢索的只是所選項目之一或所選組,但是該組中的所有項目就足夠了。 我的列表開始為空,然后使用Ajax選擇其他列表后填充。

我的JQuery change事件是這樣的:

$("#institutions").change(function() {
                $.ajax({
                    type: 'POST',
                    url: '@Url.Action("FillGroupsAndFam")',
                    dataType: 'json',
                    data: { institutionId: Number($("#institutions").val()) },
                    success: function(model) {
                        $("#family-select").empty();
                        var $prevGroup, prevGroupName;
                        $.each(model.FamilyGroups, function() {                              
                            if (prevGroupName != this.Group.Name) {
                                $prevGroup = $(document.createElement("optgroup"))
                                .prop("label", this.Group.Name)
                                .appendTo("#family-select");
                            }
                                $(document.createElement("option"))
                                    .val(this.Value)
                                    .text(this.Text)
                                    .appendTo("#family-select");
                            prevGroupName = this.Group.Name;
                        });
                        $("#family-select").multiselect('rebuild');

這將調用FillGroupsAndFam ,它會像這樣填充我的列表:

     var famList = (from f in fam
        let famGroup = new SelectListGroup {Name = f.FamilyGroupName}
        from id in f.UserIds
        select new SelectListItem
        {
            Text = UserManager.FindById(id).UserName, Value = id, Disabled = true, Group = famGroup
        }).ToList();

在我看來,我正在顯示以下列表:

    @Html.LabelFor(m => m.CreateModel.FamilyGroups)
    @Html.ListBoxFor(m => m.CreateModel.SelectedFamilyGroupId, Model.CreateModel.FamilyGroups, new {@class="form-control", @id="family-select"})

而我的ViewModel具有:

[Display(Name="Family in Study")]
public IEnumerable<SelectListItem> FamilyGroups { get; set; }
public IEnumerable<SelectListItem> SelectedFamilyGroupId { get; set; }

我通過將SelectedFamilyGroupId類型更改為IEnumerable<string>來解決此問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM