public RoomSortViewModel(RoomSortState sortOrder) { TypeSort = sortOrder == RoomSortState.TypeAsc ? RoomSortState.TypeDesc : RoomSortState.TypeAsc; CapacitySort = sortOrder == RoomSortState.CapacityAsc ? RoomSortState.CapacityDesc : RoomSortState.CapacityAsc; DescriptionSort = sortOrder == RoomSortState.DescriptionAsc ? RoomSortState.DescriptionDesc : RoomSortState.DescriptionAsc; PriceSort = sortOrder == RoomSortState.PriceAsc ? RoomSortState.PriceDesc : RoomSortState.PriceAsc; Current = sortOrder; }
public async Task <IActionResult> Index(string type, int page = 1, RoomSortState sortOrder = RoomSortState.TypeAsc) { int pageSize = 5; //фильтрация IQueryable <Room> rooms = db.Rooms; if (!string.IsNullOrEmpty(type)) { rooms = rooms.Where(p => p.RoomType.Contains(type)); } // сортировка switch (sortOrder) { case RoomSortState.TypeDesc: rooms = rooms.OrderByDescending(s => s.RoomType); break; case RoomSortState.CapacityAsc: rooms = rooms.OrderBy(s => s.RoomCapacity); break; case RoomSortState.CapacityDesc: rooms = rooms.OrderByDescending(s => s.RoomCapacity); break; case RoomSortState.DescriptionAsc: rooms = rooms.OrderBy(s => s.RoomDescription); break; case RoomSortState.DescriptionDesc: rooms = rooms.OrderByDescending(s => s.RoomDescription); break; case RoomSortState.PriceAsc: rooms = rooms.OrderBy(s => s.RoomPrice); break; case RoomSortState.PriceDesc: rooms = rooms.OrderByDescending(s => s.RoomPrice); break; default: rooms = rooms.OrderBy(s => s.RoomType); break; } // пагинация var count = await rooms.CountAsync(); var items = await rooms.Skip((page - 1) *pageSize).Take(pageSize).ToListAsync(); // формируем модель представления RoomsViewModel viewModel = new RoomsViewModel { PageViewModel = new PageViewModel(count, page, pageSize), RoomSortViewModel = new RoomSortViewModel(sortOrder), RoomFilterViewModel = new RoomFilterViewModel(type), Rooms = items }; return(View(viewModel)); }