Ejemplo n.º 1
0
        public IActionResult Index(SortState sortState = SortState.CarModelsNameAsc, int page = 1)
        {
            CarModelsFilterViewModel filter = HttpContext.Session.Get <CarModelsFilterViewModel>(filterKey);

            if (filter == null)
            {
                filter = new CarModelsFilterViewModel {
                    CarModelName = string.Empty, CarModelDescription = string.Empty, CarMarkName = string.Empty
                };
                HttpContext.Session.Set(filterKey, filter);
            }

            string modelKey = $"{typeof(CarModel).Name}-{page}-{sortState}-{filter.CarModelName}-{filter.CarModelDescription}";

            if (!cache.TryGetValue(modelKey, out CarModelViewModel model))
            {
                model = new CarModelViewModel();

                IQueryable <CarModel> carModels = GetSortedEntities(sortState, filter.CarModelName, filter.CarModelDescription);

                int count    = carModels.Count();
                int pageSize = 10;
                model.PageViewModel = new PageViewModel(page, count, pageSize);

                model.Entities                 = count == 0 ? new List <CarModel>() : carModels.Skip((model.PageViewModel.CurrentPage - 1) * pageSize).Take(pageSize).ToList();
                model.SortViewModel            = new SortViewModel(sortState);
                model.CarModelsFilterViewModel = filter;

                cache.Set(modelKey, model);
            }

            return(View(model));
        }
Ejemplo n.º 2
0
        public IActionResult Index(CarModelsFilterViewModel filterModel, int page)
        {
            CarModelsFilterViewModel filter = HttpContext.Session.Get <CarModelsFilterViewModel>(filterKey);

            if (filter != null)
            {
                filter.CarModelName        = filterModel.CarModelName;
                filter.CarModelDescription = filterModel.CarModelDescription;

                HttpContext.Session.Remove(filterKey);
                HttpContext.Session.Set(filterKey, filter);
            }

            return(RedirectToAction("Index", new { page }));
        }