2016-01-21 121 views
0

我正在嘗試從我的多選列表中獲取所選項目/項目。我真正關心的是所選擇的項目之一或選定的組,但組中的所有項目都已足夠。我的列表開始爲空,然後在使用Ajax選擇不同列表後填充。從列表框中獲取所選項目(使用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; } 

回答

0

我解決了這個改變SelectedFamilyGroupId類型IEnumerable<string>

相關問題