public async Task <IActionResult> Sort(string sort) { if (sort == null) { return(RedirectToAction("Index")); } IQueryable <Car> query = _context.Car.AsQueryable(); if (sort != null && sort.Trim().ToUpper() == "ASC") { query = _context.Car.OrderBy(p => p.ReleaseDate); } if (sort != null && sort.Trim().ToUpper() == "DESC") { query = _context.Car.OrderByDescending(p => p.ReleaseDate); } var SortCar = new CareSizeViewModel { Cars = await query.ToListAsync() }; return(View("Index", SortCar)); }
// GET: Cars public async Task <IActionResult> Index(string carSize, string searchString) { IQueryable <string> genreQuery = from m in _context.Car orderby m.Size select m.Size; var cars = from m in _context.Car select m; if (!string.IsNullOrEmpty(searchString)) { cars = cars.Where(s => s.Name.Contains(searchString)); } if (!string.IsNullOrEmpty(carSize)) { cars = cars.Where(x => x.Size == carSize); } var CarSizeVM = new CareSizeViewModel { Sizes = new SelectList(await genreQuery.Distinct().ToListAsync()), Cars = await cars.ToListAsync() }; return(View(CarSizeVM)); }