public ActionResult Index(int Page = 1, int PageSize = 25, string FilterBy = "all", string SearchTerm = null)
        {
            logger.Debug("User " + WebSecurity.GetUserId(User.Identity.Name) +
                         " \"" + User.Identity.Name + "\" visited TournamentsManagement/Tournament/Index");

            ManageTournamentsViewModel viewModel = new ManageTournamentsViewModel();

            viewModel.FilterBy   = FilterBy;
            viewModel.SearchTerm = SearchTerm;

            if (System.Web.HttpContext.Current.Request.HttpMethod == "POST")
            {
                Page = 1;
            }

            if (PageSize == 0)
            {
                PageSize = 25;
            }

            viewModel.PageSize = PageSize;

            if (!string.IsNullOrEmpty(FilterBy))
            {
                if (FilterBy == "all" || string.IsNullOrEmpty(SearchTerm))
                {
                    viewModel.PaginatedTournamentList = repository.Tournaments
                                                        .OrderByDescending(t => t.TournamentID)
                                                        .ToPaginatedList <Tournament>(Page, PageSize);
                }
                else if (!string.IsNullOrEmpty(SearchTerm))
                {
                    if (FilterBy == "name")
                    {
                        viewModel.PaginatedTournamentList = repository.Tournaments
                                                            .Where(t => t.Name.ToLower().IndexOf(SearchTerm.ToLower()) != -1)
                                                            .OrderByDescending(t => t.TournamentID)
                                                            .ToPaginatedList <Tournament>(Page, PageSize);
                    }
                }
            }

            return(View(viewModel));
        }
        /// <summary>
        /// Return two lists:
        ///   1)  a list of Users not bound to the tournament
        ///   2)  a list of Users granted to the tournament
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public ActionResult BindUsersToTournament(int TournamentID = -1, int Page = 1, int PageSize = 25, string FilterBy = "all", string SearchTerm = null)
        {
            logger.Debug("User " + WebSecurity.GetUserId(User.Identity.Name) +
                         " \"" + User.Identity.Name + "\" visited TournamentsManagement/Tournament/BindProblemsToTournament");

            if (TournamentID == -1)
            {
                ManageTournamentsViewModel viewModel = new ManageTournamentsViewModel();
                viewModel.FilterBy   = FilterBy;
                viewModel.SearchTerm = SearchTerm;

                if (System.Web.HttpContext.Current.Request.HttpMethod == "POST")
                {
                    Page = 1;
                }

                if (PageSize == 0)
                {
                    PageSize = 25;
                }

                viewModel.PageSize = PageSize;

                if (!string.IsNullOrEmpty(FilterBy))
                {
                    if (FilterBy == "all" || string.IsNullOrEmpty(SearchTerm))
                    {
                        viewModel.PaginatedTournamentList = repository.Tournaments
                                                            .OrderByDescending(t => t.TournamentID)
                                                            .ToPaginatedList <Tournament>(Page, PageSize);
                    }
                    else if (!string.IsNullOrEmpty(SearchTerm))
                    {
                        string query = SearchTerm + "%";
                        if (FilterBy == "name")
                        {
                            viewModel.PaginatedTournamentList = repository.Tournaments
                                                                .Where(t => t.Name.ToLower().IndexOf(SearchTerm.ToLower()) != -1)
                                                                .OrderByDescending(t => t.TournamentID)
                                                                .ToPaginatedList <Tournament>(Page, PageSize);
                        }
                    }
                }
                return(View("UsersForTournament", viewModel));
            }

            BindUsersToTournamentViewModel model = new BindUsersToTournamentViewModel();
            Tournament tournament = repository.Tournaments
                                    .FirstOrDefault(t => t.TournamentID == TournamentID);

            if (tournament == null)
            {
                logger.Warn("Tournament with id = " + TournamentID + " not found");
                throw new HttpException(404, "Tournament not found");
            }

            model.TournamentID   = tournament.TournamentID;
            model.TournamentName = tournament.Name;

            // AsEnumerable() need, because Except() for IQueryable work in DB
            // and return 'not supported' (really hard to work) data.
            if (tournament.Users != null)
            {
                model.AvailableUsers = new SelectList(repository.Users.AsEnumerable().Except(tournament.Users), "UserId", "UserName");
                model.BoundUsers     = new SelectList(tournament.Users, "UserId", "UserName");
            }
            else
            {
                model.AvailableUsers = new SelectList(repository.Problems.AsEnumerable(), "UserId", "UserName");
                model.BoundUsers     = null;
            }

            return(View(model));
        }