// GET: Services public async Task <IActionResult> Index(SortState sortState = SortState.ServiceCostAsc, int page = 1) { ServicesFilterViewModel filter = HttpContext.Session.Get <ServicesFilterViewModel>(filterKey); if (filter == null) { filter = new ServicesFilterViewModel { Name = string.Empty }; HttpContext.Session.Set(filterKey, filter); } string key = $"{typeof(Service).Name}-{page}-{sortState}-{filter.Name}"; if (!_cache.TryGetValue(key, out ServiceViewModel model)) { model = new ServiceViewModel(); IQueryable <Service> services = getSortedServices(sortState, filter.Name); int count = services.Count(); model.PageViewModel = new PageViewModel(page, count, pageSize); model.Services = count == 0 ? new List <Service>() : services.Skip((model.PageViewModel.PageIndex - 1) * pageSize).Take(pageSize).ToList(); model.SortViewModel = new SortViewModel(sortState); model.ServicesFilterViewModel = filter; _cache.Set(key, model); } return(View(model)); }
public IActionResult Index(SortState sortState = SortState.ServicesNameAsc, int page = 1) { ServicesFilterViewModel filter = HttpContext.Session.Get <ServicesFilterViewModel>(filterKey); if (filter == null) { filter = new ServicesFilterViewModel { ServiceName = string.Empty, ServiceDescription = string.Empty, ServicePrice = default }; HttpContext.Session.Set(filterKey, filter); } string modelKey = $"{typeof(Service).Name}-{page}-{sortState}-{filter.ServiceName}-{filter.ServiceDescription}-{filter.ServicePrice}"; if (!cache.TryGetValue(modelKey, out ServiceViewModel model)) { model = new ServiceViewModel(); IQueryable <Service> services = GetSortedEntities(sortState, filter.ServiceName, filter.ServicePrice, filter.ServiceDescription); int count = services.Count(); int pageSize = 10; model.PageViewModel = new PageViewModel(page, count, pageSize); model.Entities = count == 0 ? new List <Service>() : services.Skip((model.PageViewModel.CurrentPage - 1) * pageSize).Take(pageSize).ToList(); model.SortViewModel = new SortViewModel(sortState); model.ServicesFilterViewModel = filter; cache.Set(modelKey, model); } return(View(model)); }
public IActionResult Index(ServicesFilterViewModel filterModel, int page) { ServicesFilterViewModel filter = HttpContext.Session.Get <ServicesFilterViewModel>(filterKey); if (filter != null) { filter.Name = filterModel.Name; HttpContext.Session.Remove(filterKey); HttpContext.Session.Set(filterKey, filter); } return(RedirectToAction("Index", new { page })); }