public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int, string> pageUrl) { StringBuilder result = new StringBuilder(); for (int i = 1; i <= pagingInfo.TotalPages; i++) { TagBuilder tag = new TagBuilder("a"); tag.MergeAttribute("href", pageUrl(i)); tag.InnerHtml = i.ToString(); if (i == pagingInfo.CurrentPage) tag.AddCssClass("selected"); result.Append(tag.ToString()); } return MvcHtmlString.Create(result.ToString()); }
public ViewResult List(string category, int page = 1) { productsOfCategory = repository.Products.Where(p => category == null || p.Category.CategoryName == category); productsForDisplay = productsOfCategory.OrderBy(x => x.ProductID).Skip((page - 1) * PageSize).Take(PageSize); pagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = productsOfCategory.Count() }; logger.Info("List"); ProductListViewModel viewModel = new ProductListViewModel { Products = productsForDisplay, PagingInfo = pagingInfo }; currentCategory = category; viewModel.CurrentCategory = category; return View(viewModel); }
public ActionResult SearchList(string category, int page = 1) { IEnumerable<Product> products = (IEnumerable<Product>)TempData["Products"]; if (products == null) return RedirectToAction("List"); pagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = products.Count() }; logger.Info("List"); ProductListViewModel viewModel = new ProductListViewModel { Products = products, PagingInfo = pagingInfo }; currentCategory = category; viewModel.CurrentCategory = category; return View("List", viewModel); }
public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, string category) { StringBuilder result = new StringBuilder(); for (int i = 1; i <= pagingInfo.TotalPages; i++) { TagBuilder tag = new TagBuilder("a"); if (category == null) tag.MergeAttribute("href", String.Format("/Page{0}", i)); else tag.MergeAttribute("href", String.Format("/{0}/Page{1}", category, i)); tag.InnerHtml = i.ToString(); if (i == pagingInfo.CurrentPage) { tag.AddCssClass("selected"); tag.AddCssClass("btn-primary"); } tag.AddCssClass("btn btn-default"); result.Append(tag.ToString()); } return MvcHtmlString.Create(result.ToString()); }