public ViewResult List(Cart cart, string category, int page = 1) { var products = _productRepository.Products .Where(p => category == null || p.Category == category) .OrderBy(p => p.ProductID) .Skip((page - 1)*PageSize) .Take(PageSize); string carttotal = cart.ComputeTotalValue().ToString("c"); int quantity = cart.Lines.Sum(x => x.Quantity); ProductListViewModel viewModel = new ProductListViewModel { Products = products, PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = category == null ? _productRepository.Products.Count() : _productRepository.Products.Count(x => x.Category == category) }, CurrentCategory = category, CartTotal = carttotal, CartQuantity = quantity }; return View(viewModel); }
public ActionResult List(string category, string sort, int page = 1) { ProductListViewModel result = new ProductListViewModel(); var ProdQuery = repository.Products .Where(m => category == null || m.Category == category); if (sort != null) { switch (sort.ToLower()) { case "name": ProdQuery=ProdQuery.OrderBy(m => m.Name); break; case "namedesc": ProdQuery = ProdQuery.OrderByDescending(m => m.Name); break; case "price": ProdQuery = ProdQuery.OrderBy(m => m.Price); break; case "pricedesc": ProdQuery = ProdQuery.OrderByDescending(m => m.Price); break; default: ProdQuery = ProdQuery.OrderBy(m => m.ProductID); break; } } else { ProdQuery = ProdQuery.OrderBy(m => m.ProductID); } result.Products = ProdQuery .Skip((page - 1) * PageSize) .Take(PageSize); result.PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = ProdQuery.Count() }; result.CurrentCategory = category; result.Sorted = sort; return View(result); }
public ViewResult List(string category, int page = 1) { productsOfCategory = repository.Products.Where(p => category == null || p.Category == category); productsForDisplay = productsOfCategory.OrderBy(x => x.ProductId).Skip((page - 1) * PageSize).Take(PageSize); pagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = productsOfCategory.Count() }; ProductListViewModel viewModel = new ProductListViewModel { Products = productsForDisplay, PagingInfo = pagingInfo }; currentCategory = category; viewModel.CurrentCategory = category; return View(viewModel); }
// GET: Product public ActionResult List(string category, int page = 1) { var productsOfCategory = this._repo.Products.Where(a => String.IsNullOrEmpty(category) || a.Category == category).OrderBy(a => a.ProductID); var products = productsOfCategory.Skip((page - 1) * _pageSize).Take(_pageSize); ProductListViewModel vm = new ProductListViewModel(); vm.PageInfo = new PageInfo() { CurrentPage = page, ItemsPerPage = _pageSize, TotalItems = productsOfCategory.Count() }; vm.Products = products.ToList(); vm.CurrentCategory = category; return View(vm); }
public ActionResult List(string category, int page = 1) { ProductListViewModel model = new ProductListViewModel { Products = repository.Products.Where(p=>category == null || p.Category == category) .OrderBy(p => p.ProductID) .Skip((page - 1) * PageSize) .Take(PageSize), PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = category == null ? repository.Products.Count() : repository.Products.Where(e => e.Category == category).Count() }, CurrentCategory = category }; return View(model); }
public ViewResult List(string category, int page=1) { var product = (IEnumerable<Product>) _productRepository.Products.Where(p => category == null || p.Category == category); ProductListViewModel model = new ProductListViewModel() { Products = product.OrderBy(p => p.ProductID).Skip((page -1)*PageSize) .Take(PageSize), PageInfo = new PageInfo() { CurrentPage = page, ItemPerPage = PageSize, TotalItem = product.Count() }, CurrentCategory = category }; return View(model); }
public ViewResult List(string category, int page = 1) { ProductListViewModel model = new ProductListViewModel { Products = repository.Products .Where(x => string.IsNullOrEmpty(category) || x.Category == category) .OrderBy(x => x.ProductID) .Skip((page - 1) * PageSize) .Take(PageSize), PaginInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = repository.Products.Count(x => string.IsNullOrEmpty(category) ? true : x.Category == category) }, CurrentCategory = category }; return View(model); }
public ViewResult List([DefaultValue(1)] int page) { // Now I understand what IQueryable is // I think that LINQ adds a static extension method // which resolves the type you send? And then it // configures whatever type it can configure to be 'queryable' // or to allow query expressions to be written on it // In this case, the Products property of IProductsRepository // returns IQueryable<Products>, and that IQueryable collection // has these methods on it // Remember when you pass a collection to this // view method like this, you're passing data to the // Model object // Grab the products list so we can pass the total // number of products to the pagingInfo view model IQueryable<Product> products = productsRepository.Products; // Now query that list of products to get the products // to be viewed on the current page IList<Product> productList = products.Skip((page - 1) * PageSize).Take(PageSize).ToList(); PagingInfo pagingInfo = new PagingInfo { CurrentPage = page, TotalItems = products.Count(), ItemsPerPage = PageSize }; var viewModel = new ProductListViewModel { PagingInfo = pagingInfo, Products = productList }; return View(viewModel); }
public ViewResult List(string category, int page = 1) { Func<Product, bool> categoriesFilter = p => category == null || p.Category == category; var viewModel = new ProductListViewModel { Products = repository .Products .Where(categoriesFilter) .OrderBy(p => p.ProductID) .Skip((page - 1) * PageSize) .Take(PageSize), PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = repository.Products.Count(categoriesFilter), }, CurrentCategory = category, }; return View(viewModel); }