public IActionResult ViewByCategories(string[] categories, int?page) { // I have not figured out how to convert a string array to routeparam // in mvc6 yet so the categories may be passed as a single csv string if (categories != null) { if ((categories.Length == 1) && (categories[0].Contains(","))) { categories = categories[0].Split(','); } } var model = new ViewByCategoriesViewModel(); model.Categories = categories ?? new string[0]; int currentPageIndex = page.HasValue ? page.Value - 1 : 0; model.Products = this.allProducts.Where(p => model.Categories.Contains(p.Category)).ToPagedList(currentPageIndex, DefaultPageSize); model.AvailableCategories = this.allCategories; model.Paging.CurrentPage = page.HasValue ? page.Value : 1; model.Paging.ItemsPerPage = DefaultPageSize; model.Paging.TotalItems = model.Products.TotalItemCount; model.Paging.ShowFirstLast = true; return(View("ProductsByCategories", model)); }
public ActionResult ViewByCategories(string[] categories, int?page) { var model = new ViewByCategoriesViewModel(); model.Categories = categories ?? new string[0]; int currentPageIndex = page.HasValue ? page.Value - 1 : 0; model.Products = this.allProducts.Where(p => model.Categories.Contains(p.Category)).ToPagedList(currentPageIndex, DefaultPageSize); model.AvailableCategories = this.allCategories; return(View("ProductsByCategories", model)); }
public IActionResult ViewByCategories(string[] categories, int?page) { // I have not figured out how to convert a string array to routeparam // in mvc6 yet so the categories may be passed as a single csv string if (categories != null) { if ((categories.Length == 1) && (categories[0].Contains(","))) { categories = categories[0].Split(','); } } var currentPageNum = page.HasValue ? page.Value : 1; var offset = (DefaultPageSize * currentPageNum) - DefaultPageSize; var model = new ViewByCategoriesViewModel(); model.Categories = categories ?? new string[0]; int currentPageIndex = page.HasValue ? page.Value - 1 : 0; var filtered = this.allProducts.Where(p => model.Categories.Contains(p.Category) ).ToList(); model.Products.Data = filtered .Skip(offset) .Take(DefaultPageSize) .ToList(); model.AvailableCategories = this.allCategories; model.Products.PageNumber = currentPageNum; model.Products.PageSize = DefaultPageSize; model.Products.TotalItems = filtered.Count; return(View("ProductsByCategories", model)); }