// GET: AdminPanel public async Task <IActionResult> Index( int?contestId, int?nominationId, int page = 1, PostsSortState sortOrder = PostsSortState.RatingAsc) { int pageSize = 10; IQueryable <Post> posts = _context.Posts .Where(x => x.ContestId == contestId) .Include(x => x.Author) .Include(x => x.Likes) .Include(x => x.Nomination); //Фильтрация if (nominationId != null && nominationId != 0) { posts = posts.Where(x => x.NominationId == nominationId); } //Сортировка switch (sortOrder) { case PostsSortState.RatingAsc: posts = posts.OrderBy(x => x.Rating); break; case PostsSortState.RatingDesc: posts = posts.OrderByDescending(x => x.Rating); break; case PostsSortState.NameAsc: posts = posts.OrderBy(x => x.Title); break; case PostsSortState.NameDesc: posts = posts.OrderByDescending(x => x.Title); break; } //Пагинация var count = await posts.CountAsync(); var items = await posts.Skip((page - 1) *pageSize).Take(pageSize).ToListAsync(); PostsIndexViewModel viewModel = new PostsIndexViewModel() { PageViewModel = new PostsPageViewModel(count, page, pageSize), SortViewModel = new PostsSortViewModel(sortOrder), FilterViewModel = new PostFilterViewModel(_context.Nominations.Where(x => x.ContestId == contestId).ToList(), nominationId), Posts = items, HelpNamePost = new Post() }; ViewBag.ContestId = contestId; ViewBag.ContestName = _context.Contests.FirstOrDefaultAsync(x => x.Id == contestId).Result.Name; return(View(viewModel)); }
} // текущее значение сортировки public PostsSortViewModel(PostsSortState sortOrder) { NameSort = sortOrder == PostsSortState.NameAsc ? PostsSortState.NameDesc : PostsSortState.NameAsc; RatingSort = sortOrder == PostsSortState.RatingAsc ? PostsSortState.RatingDesc : PostsSortState.RatingAsc; Current = sortOrder; }