public ViewResult List(int page = 1, string category = null)
{
const int pageSize = 3;
IEnumerable products;
int currentPage;
int pageCount;

using (_repository.NewSession())
{
IQueryable queryableProducts = !string.IsNullOrEmpty(category)
? _repository.AsQueryable().Where(x => x.Category == category)
: _repository.AsQueryable();
pageCount = Utility.DivideAndRoundUp(queryableProducts.Count(), pageSize);
if (page pageCount)
{
page = 1;
}
currentPage = page;
products = queryableProducts.Skip((page - 1) * pageSize).Take(pageSize);
}

return View(new ProductListModel(category, products, currentPage, pageCount));
}

Advertisements