【问题标题】:Get selected items from ListBox (populated with Ajax)从 ListBox 中获取所选项目(使用 Ajax 填充)
【发布时间】:2016-01-22 02:37:13
【问题描述】:

我正在尝试从我的多选列表中获取选定的项目。我真正关心的是检索所选项目或所选组之一,但组中的所有项目就足够了。我的列表一开始是空的,然后在使用 Ajax 选择不同的列表后填充。

我的 JQuery 更改事件是这样的:

$("#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; }

【问题讨论】:

    标签: c# jquery ajax asp.net-mvc bootstrap-multiselect


    【解决方案1】:

    我通过将 SelectedFamilyGroupId 类型更改为 IEnumerable&lt;string&gt; 解决了这个问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-28
      • 1970-01-01
      • 2014-04-24
      • 1970-01-01
      相关资源
      最近更新 更多