private IQueryable<LaptopViewModel> GetLaptops(LaptopSearchModel laptopSearchModel, int page) { var laptops = this.Data.Laptops.All() .OrderByDescending(l => l.Votes.Count) .Where(l => laptopSearchModel.ByModel == null || l.Model.Contains(laptopSearchModel.ByModel)) .Where(l => laptopSearchModel.ByManufacturer == null || l.ManufacturerId == laptopSearchModel.ByManufacturer) .Where(l => laptopSearchModel.ByPrice == null || l.Price <= laptopSearchModel.ByPrice) .Skip((page - 1) * PAGE_SIZE).Take(PAGE_SIZE) .Select(LaptopViewModel.FromLaptop); return laptops; }
private decimal GetPageNumbers(LaptopSearchModel laptopSearchModel) { var pages = Math.Ceiling((decimal)this.Data.Laptops.All() .Where(l => laptopSearchModel.ByModel == null || l.Model.Contains(laptopSearchModel.ByModel)) .Where(l => laptopSearchModel.ByManufacturer == null || l.ManufacturerId == laptopSearchModel.ByManufacturer) .Where(l => laptopSearchModel.ByPrice == null || l.Price <= laptopSearchModel.ByPrice) .Count() / PAGE_SIZE); return pages; }
public ActionResult Search(LaptopSearchModel laptopSearchModel) { laptopSearchModel.Manufacturers = this.Data.Manufacturers.All().Select(m => new SelectListItem { Text = m.Name, Value = m.Id.ToString(), Selected = false }); //var laptopViewModel = new LaptopViewModel //{ // LaptopSearchModel = laptopSearchModel //}; if (ModelState.IsValid) { var page = laptopSearchModel.Page.GetValueOrDefault(1); var pages = GetPageNumbers(laptopSearchModel); ViewBag.Pages = pages; laptopSearchModel.Laptops = GetLaptops(laptopSearchModel, page); return View(laptopSearchModel); } return View(laptopSearchModel); }