locked
Disabled not working in select2 multiselect dropdown? asp.net core mvc RRS feed

  • Question

  • User342972608 posted

    Hi,

    I am using select2 multiselect dropdown which are working fine but i am trying to bind values with disabled but disabled not working, i debug code and check list contains disabled=true for some items but in select2 multiselect dropdown not working.

    Controller

       public async Task<IActionResult> UpdateTeams(int resultid,int groupid)
            {
                List<int> listPlayersA = new List<int>();
                List<int> listPlayersB = new List<int>();
                var vm = new ResultViewModel(group);
                vm.AvailablePlayersB = vm.AvailablePlayers;
                var AvailablePlayersBList = vm.AvailablePlayersB.ToList(); // Get IEnumerable as List
                var AvailablePlayersAList = vm.AvailablePlayers.ToList(); // Get IEnumerable as List
                var TeamMembersA = _context.GamesPlayed.Where(u => u.ResultId == resultid && u.TeamName==TeamsName[0].ToString() ).ToList();
                var TeamMembersB = _context.GamesPlayed.Where(u => u.ResultId == resultid && u.TeamName == TeamsName[1].ToString()).ToList();
                foreach(var playersA in TeamMembersA)
                {
                    listPlayersA.Add(playersA.UserId);
                    foreach (var itemB in AvailablePlayersBList)
                    {
                        if (itemB.Value == playersA.UserId.ToString())
                        {
                            itemB.Disabled = true;
                            break;
                        }
                    }
                }
    
                foreach (var playersB in TeamMembersB)
                {
                    listPlayersB.Add(playersB.UserId);
                    foreach (var itemA in AvailablePlayersAList)
                    {
                        if (itemA.Value == playersB.UserId.ToString())
                        {
                            itemA.Disabled = true;
                            break;
                        }
                    }
                }
    
                if (group == null)
                {
                    return RedirectToAction("Not Found");
                }
                vm.SelectedAvailablePlayersA = listPlayersA;
                vm.SelectedAvailablePlayersB = listPlayersB;
                vm.AvailablePlayers = AvailablePlayersAList; //list of players //which contains some items disabled=true,i debuge and check its //fine
                vm.AvailablePlayersB = AvailablePlayersBList;//list of players ///which contains some items disabled=true,i debuge and check its //fine
                return View(vm);
            }

    View

    @model ResultViewModel
    @{
        ViewData["Title"] = "UpdateTeams";
        Layout = "~/Views/Shared/_AdminLayout.cshtml";
    }
      <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
        <link href="https://cdn.jsdelivr.net/npm/select2@4.0.13/dist/css/select2.min.css" rel="stylesheet" />
    
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/select2@4.0.13/dist/js/select2.min.js"></script>
    
    
        <style>
            body {
                background-color: #ebeef4;
            }
        </style>
    
    }
    <div id="content" class="app-content">
        <h4>@TempData["GroupName"]</h4>
        <form>
            <label>@TempData["TeamA"]</label>
    
            @Html.ListBoxFor(m => m.SelectedAvailablePlayersA,
             new MultiSelectList(Model.AvailablePlayers, "Value", "Text"),
              new { @style = "width: 290px;height:150px;", @class = "select2 select2-multiple", data_toggle = "dropdown", data_placeholder = "Choose Team B Players", data_style = "btn-default", id = "PlayersA", multiple = "multiple" })
            <br />
            <label>@TempData["TeamB"]</label>
    
            @Html.ListBoxFor(m => m.SelectedAvailablePlayersB,
            new MultiSelectList(Model.AvailablePlayersB, "Value", "Text"),
             new { @style = "width: 290px;height:150px;", @class = "select2 select2-multiple", data_toggle = "dropdown", data_placeholder = "Choose Team B Players", data_style = "btn-default", id = "PlayersB", multiple = "multiple" })
    
        </form>
    </div>
    

    Friday, September 11, 2020 7:00 AM

Answers

  • User-474980206 posted

    just pass the selected and disabled lists

    new MultiSelectList(Model.AvailablePlayers, 
        "Value", 
        "Text",
        null,
        Model.AvailablePlayers.Where(p => p.Disabled)
    ),

    or just build the list yourself

     @Html.ListBoxFor(m => m.SelectedAvailablePlayersA,
         Model.AvailablePlayers.Select( p => new SelectListItem {Value = p.Value, Text = p.Text, Disabled = p.Disabled}),
    
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 11, 2020 6:17 PM

All replies