public ProductPagingViewModel GetProducts(int categoryId, int pageSize, int pageIndex) { var result = new ProductPagingViewModel(); var totalProducts = _context.ProductCategories.Where(c => !c.IsDeleted && c.CategoryId == categoryId && !c.Product.IsDeleted).Select(p => p.Product).OrderByDescending(p => p.DateCreated); result.Total = totalProducts.Count(); result.TotalPages = (int)Math.Ceiling((double)totalProducts.Count() / pageSize); var products = totalProducts.Skip(pageSize * pageIndex).Take(pageSize).ToList(); result.Results = _mapper.Map <List <Product>, List <ProductViewModel> >(products); return(result); }
public ProductPagingViewModel SearchProducts(string name, string code, int pageSize, int pageIndex) { var result = new ProductPagingViewModel(); if (code.IsNullOrEmpty()) { code = ""; } if (!string.IsNullOrEmpty(code) && code.Length <= 1) { code = ""; } if (string.IsNullOrEmpty(name) && string.IsNullOrEmpty(code)) { var totalProducts = _context.Products.Where(i => !i.IsDeleted).Count(); var pagingProducts = _context.Products.Where(i => !i.IsDeleted).Skip(pageSize * pageIndex).Take(pageSize).OrderByDescending(i => i.DateUpdated).ToList(); result.Total = totalProducts; result.TotalPages = (int)Math.Ceiling((double)result.Total / pageSize); result.Results = _mapper.Map <List <Product>, List <ProductViewModel> >(pagingProducts); } else if (string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(code)) { var pagingProducts = _context.Products.Where(i => !i.IsDeleted && EF.Functions.Like(i.Code, $"{code}%")).Skip(pageSize * pageIndex).Take(pageSize).OrderByDescending(i => i.DateUpdated).ToList(); var totalProducts = _context.Products.Where(i => !i.IsDeleted && EF.Functions.Like(i.Code, $"{code}%")).Count(); result.Total = totalProducts; result.TotalPages = (int)Math.Ceiling((double)result.Total / pageSize); result.Results = _mapper.Map <List <Product>, List <ProductViewModel> >(pagingProducts); } else if (!string.IsNullOrEmpty(name) && string.IsNullOrEmpty(code)) { var totalProducts = _context.Products.Where(i => !i.IsDeleted && EF.Functions.FreeText(i.Name, name)).Count(); var pagingProducts = _context.Products.Where(i => !i.IsDeleted && EF.Functions.FreeText(i.Name, name)).Skip(pageSize * pageIndex).Take(pageSize).OrderByDescending(i => i.DateUpdated).ToList(); result.Total = totalProducts; result.TotalPages = (int)Math.Ceiling((double)result.Total / pageSize); result.Results = _mapper.Map <List <Product>, List <ProductViewModel> >(pagingProducts); } else { var totalProducts = _context.Products.Where(i => !i.IsDeleted && EF.Functions.FreeText(i.Name, name) && EF.Functions.Like(i.Code, $"{code}%")).Count(); var pagingProducts = _context.Products.Where(i => !i.IsDeleted && EF.Functions.FreeText(i.Name, name) && EF.Functions.Like(i.Code, $"{code}%")).Skip(pageSize * pageIndex).Take(pageSize).OrderByDescending(i => i.DateUpdated).ToList(); result.Total = totalProducts; result.TotalPages = (int)Math.Ceiling((double)result.Total / pageSize); result.Results = _mapper.Map <List <Product>, List <ProductViewModel> >(pagingProducts); } return(result); }
// GET: Product public async Task <ActionResult> Index(string name, int price, int?pageNumber = 1, int size = 5) { if (name != null && name != string.Empty) { HttpContext.Session.SetString("name", name); } var products = await _productService.GetProductsAsync(HttpContext.Session.GetString("name"), (int)pageNumber, size); var model = new ProductPagingViewModel { Products = products, CurrentPage = (int)pageNumber, PageSize = size, Count = _productService.GetCountAsync().GetAwaiter().GetResult() }; return(View(model)); }
public async Task <IActionResult> Index(int page = 1) { int pageSize = 3; // количество элементов на странице var items = _ProductData.GetProductsByPage(page, pageSize)?.Result; var count = _ProductData.GetProductsCount()?.Result; PageViewModel pageViewModel = new PageViewModel(count.Value, page, pageSize); ProductPagingViewModel viewModel = new ProductPagingViewModel { PageViewModel = pageViewModel, Products = items }; return(View(viewModel)); }
public IActionResult Index(int?page) { var model = new ProductPagingViewModel(); int pageSize = 10; var currentPageNum = page.HasValue ? page.Value : 1; var offset = (pageSize * currentPageNum) - pageSize; model.Products = ProductModel.GetProducts() .Skip(offset).Take(pageSize).OrderBy(p => p.Name).ToList(); model.Paging.CurrentPage = currentPageNum; model.Paging.ItemsPerPage = pageSize; model.Paging.TotalItems = ProductModel.GetProducts().Count; return(View(model)); }