0

我正在使用多选下拉菜单的引导selectpicker来过滤mvc5 web应用程序中表的项目。到目前为止一切正常,但我无法保持提交后选定的筛选器。因此,我可以读取控制器中选定的过滤器,但在此之后,只有先前选择的第一个过滤器在提交后仍显示为选中状态。我希望所有选定的过滤器仍然被选中。我怎么能达到这个?Bootstrap Selectpicker multiselect在提交后不保留选择

这里北京时间我的代码,视图模型包含:

 public MultiSelectList AvailableUser_ID { get; set; } 

     private List<string> _selectedUserId = new List<string>(); 
     public List<string> SelectedUserId 
     { 
      get { return _selectedUserId; } 
      set { _selectedUserId = value; } 
     } 

的控制器(POST):

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Index([Bind(Include = "SelectedUserId,SelectedUserInteractionTypesId")] IndexUserInteractionViewModel indexUserInteractionViewModel) 
    { 
     indexUserInteractionViewModel.UserInteractionViewModels = new List<UserInteractionViewModels>(); 
     indexUserInteractionViewModel.AvailableUser_ID = new MultiSelectList(db.AspNetUsers.ToList(), "Id", "Email", indexUserInteractionViewModel.SelectedUserId); 

    // Filter Function: selectedUserId contains all the Ids of the previously selected filters 
    foreach (string selectedUserId in indexUserInteractionViewModel.SelectedUserId) 
    { 
     if (userInteraction.AspNetUsers_Id.Equals(selectedUserId)) 
      // ... 
    } 
} 

和视图:

<script type="text/javascript"> 

    $(document).ready(function() { 
     $('.selectpicker').selectpicker(); 
    }); 

</script> 

<th>@Html.DropDownListFor(Model => Model.SelectedUserId, Model.AvailableUser_ID as MultiSelectList, new { @id = "userFilter", @class = "selectpicker", @multiple = "mulitple", data_live_search = "true" })</th> 

所以,我怎么能保持选择了选择?

不幸的是我有一点js知识,我假设我可以在js脚本中解决它。我希望这里有一些专家。谢谢!

回答

0

我的一位同事找到了解决方法,这是什么工作:

 var selectedList = @Html.Raw(Json.Encode(Model.SelectedUserId)); 
     if(selectedList.length > 0){ 
      var result = '['; 
      for (i = 0; i < selectedList.length; i++) { 
       result += '"' + selectedList[i] + '",'; 
      } 
      result = result.slice(0, -1); //remove last comma 
      result += ']'; 

      $('.selectpicker').selectpicker('val', JSON.parse(result1)); 
     }else{ 
      $('.selectpicker').selectpicker(); 
     } 
相关问题