public async Task <IActionResult> Index(string sortOrder, string currentFilter, string searchString, int?pageNumber) { ViewData["CurrentSort"] = sortOrder; ViewData["NameSortParm"] = string.IsNullOrEmpty(sortOrder) ? "name_desc" : ""; ViewData["TeamSortParm"] = sortOrder == "team" ? "team_desc" : "team"; if (searchString != null) { pageNumber = 1; } else { searchString = currentFilter; } ViewData["CurrentFilter"] = searchString; var users = await _userManager.Users.Include(u => u.Team).ToListAsync(); var userRolesViewModel = new List <UserRolesViewModel>(); foreach (ApplicationUser user in users) { UserRolesViewModel userRoleViewModel = await BuildUserRoleViewModel(user); userRolesViewModel.Add(userRoleViewModel); } if (!String.IsNullOrEmpty(searchString)) { userRolesViewModel = userRolesViewModel.Where(u => u.FullName.ToLower().Contains(searchString.ToLower())).ToList(); } switch (sortOrder) { case "name_desc": userRolesViewModel = userRolesViewModel.OrderByDescending(u => u.FullName).ToList(); break; case "team": userRolesViewModel = userRolesViewModel.OrderBy(u => u.TeamName).ToList(); break; case "team_desc": userRolesViewModel = userRolesViewModel.OrderByDescending(u => u.TeamName).ToList(); break; default: userRolesViewModel = userRolesViewModel.OrderBy(u => u.FullName).ToList(); break; } return(View(PaginatedListViewModel <UserRolesViewModel> .Create(userRolesViewModel.AsQueryable(), pageNumber ?? 1, CommonConstants.PAGE_SIZE))); }
public void Create_Page3_PageSize4() { // 1. Arrange int?pageNumber = 3; int pageSize = 4; int expectedTotalPages = 3; var expectedResult = new List <int> { 9, 0 }; // 2. Act var actualResult = PaginatedListViewModel <int> .Create(testList.AsQueryable(), pageNumber ?? 1, pageSize); // 3. Assert Assert.Equal(expectedResult.Count, actualResult.Count); Assert.Equal(expectedTotalPages, actualResult.TotalPages); Assert.Equal(pageNumber, actualResult.PageIndex); Assert.False(actualResult.HasNextPage); Assert.True(actualResult.HasPreviousPage); for (int i = 0; i < actualResult.Count; i++) { Assert.Equal(expectedResult[i], actualResult[i]); } }