public async Task <IActionResult> Index(int?type, string name, int page = 1, SortState sortOrder = SortState.NameAsc) { const int pageSize = 5; //фильтрация IQueryable <Toy> toys = db.Toys.Include(x => x.Type); if (type != null && type != 0) { toys = toys.Where(p => p.TypeId == type); } if (!string.IsNullOrEmpty(name)) { toys = toys.Where(p => p.Name.Contains(name)); } // сортировка toys = sortOrder switch { SortState.NameDesc => toys.OrderByDescending(s => s.Name), SortState.PriceAsc => toys.OrderBy(s => s.Price), SortState.PriceDesc => toys.OrderByDescending(s => s.Price), SortState.TypeAsc => toys.OrderBy(s => s.Type.Name), SortState.TypeDesc => toys.OrderByDescending(s => s.Type.Name), _ => toys.OrderBy(s => s.Name) }; // пагинация var count = await toys.CountAsync(); var items = await toys.Skip((page - 1) *pageSize).Take(pageSize).ToListAsync(); // формируем модель представления var viewModel = new IndexViewModel { PageViewModel = new PageViewModel(count, page, pageSize), SortViewModel = new SortViewModel(sortOrder), FilterViewModel = new FilterViewModel(db.ToyTypes.ToList(), type, name), Toys = items }; return(View(viewModel)); }
public async Task <IActionResult> Index(int?selectID, int?selectNumber, int?selectValue, SortState sortOrder = SortState.IDAsc, int page = 1) { var data = _baseService.GetCounters(); int pageSize = 5; if (selectID != null) { data = data.Where(k => k.ID + 8 == selectID).Select(k => k); } if (selectNumber != null) { data = data.Where(k => k.Number == selectNumber).Select(k => k); } if (selectValue != null) { data = data.Where(k => k.Value == selectValue).Select(k => k); } data = sortOrder switch { SortState.IDDesc => data.OrderByDescending(x => x.ID), SortState.NumberAsc => data.OrderBy(x => x.Number), SortState.NumberDesc => data.OrderByDescending(x => x.Number), SortState.ValueAsc => data.OrderBy(x => x.Value), SortState.ValueDesc => data.OrderByDescending(x => x.Value), _ => data.OrderBy(x => x.ID), }; var count = await data.CountAsync(); var items = await data.Skip((page - 1) *pageSize).Take(pageSize).ToListAsync(); IndexViewModel viewModel = new IndexViewModel { Counters = items, SortViewModel = new SortViewModel(sortOrder), PageViewModel = new PageViewModel(count, page, pageSize), FilterViewModel = new FilterViewModel(selectID, selectNumber, selectValue) }; return(View(viewModel)); }
public async Task <IActionResult> Index(string name, int?manufacturer, SortState sortOrder = SortState.NameAsc, int page = 1) { int pageSize = 4; IQueryable <Mobile> songs = db.Mobiles.Include(x => x.Manufacturer); if (manufacturer != null && manufacturer != 0)//brand filter { songs = songs.Where(p => p.ManufacturerId == manufacturer); } if (!String.IsNullOrEmpty(name))//song name filter { songs = songs.Where(p => p.Name.Contains(name)); } songs = sortOrder switch { SortState.NameDes => songs.OrderByDescending(s => s.Name), SortState.YearAsc => songs.OrderBy(s => s.Yaer), SortState.ManufacturerAsc => songs.OrderBy(s => s.Manufacturer.Name), SortState.ManufacturerDes => songs.OrderByDescending(s => s.Manufacturer.Name), SortState.DiagonalAsc => songs.OrderBy(s => s.Diagonal), SortState.DiagonalDes => songs.OrderByDescending(s => s.Diagonal), _ => songs.OrderBy(s => s.Name), }; var count = await songs.CountAsync(); var items = await songs.Skip((page - 1) *pageSize).Take(pageSize).ToListAsync(); IndexViewModel viewModel = new IndexViewModel { PageViewModel = new PageViewModel(count, page, pageSize), SortViewModel = new SortViewModel(sortOrder), FilterViewModel = new FilterViewModel(db.Manufacturers.ToList(), manufacturer, name), Mobile = items }; SetClearance(); return(View(viewModel)); }