public async Task <IActionResult> Index(int?category, string name, int page = 1, SortState sortOrder = SortState.NameAsc)
        {
            int pageSize = 6;

            IQueryable <Product> products = _context.Product.Include(x => x.Category);

            if (category != null && category != 0)
            {
                products = products.Where(p => p.CategoryId == category);
            }
            if (!String.IsNullOrEmpty(name))
            {
                products = products.Where(p => p.Name.Contains(name));
            }


            switch (sortOrder)
            {
            case SortState.NameDesc:
                products = products.OrderByDescending(s => s.Name);
                break;

            case SortState.PriceAsc:
                products = products.OrderBy(s => s.Price);
                break;

            case SortState.PriceDesc:
                products = products.OrderByDescending(s => s.Price);
                break;

            case SortState.CategoryAsc:
                products = products.OrderBy(s => s.Category.Name);
                break;

            case SortState.CategoryDesc:
                products = products.OrderByDescending(s => s.Category.Name);
                break;

            default:
                products = products.OrderBy(s => s.Name);
                break;
            }

            var count = await products.CountAsync();

            var items = await products.Skip((page - 1) *pageSize).Take(pageSize).ToListAsync();


            IndexSortViewModel viewModel = new IndexSortViewModel
            {
                PageViewModel   = new PageViewModel(count, page, pageSize),
                SortViewModel   = new SortViewModel(sortOrder),
                FilterViewModel = new FilterViewModel(_context.Category.ToList(), category, name),
                Products        = items
            };

            return(View(viewModel));
        }
示例#2
0
        public async Task <IActionResult> Sort(SortState sortOrder = SortState.NameAsc)
        {
            IIncludableQueryable <MetanitExampleCoreMVC.Models.User, Company> users = dbUsers.Users.Include(x => x.Company);
            IQueryable <MetanitExampleCoreMVC.Models.User> usersResult;

            ViewData["NameSort"]    = sortOrder == SortState.NameAsc ? SortState.NameDesc : SortState.NameAsc;
            ViewData["AgeSort"]     = sortOrder == SortState.AgeAsc ? SortState.AgeDesc : SortState.AgeAsc;
            ViewData["CompanySort"] = sortOrder == SortState.CompanyAsc ? SortState.CompanyDesc : SortState.CompanyAsc;

            switch (sortOrder)
            {
            case SortState.NameDesc:
                usersResult = users.OrderByDescending(s => s.Name);
                break;

            case SortState.AgeAsc:
                usersResult = users.OrderBy(s => s.Name);
                break;

            case SortState.AgeDesc:
                usersResult = users.OrderByDescending(s => s.Name);
                break;

            case SortState.CompanyAsc:
                usersResult = users.OrderBy(s => s.Name);
                break;

            case SortState.CompanyDesc:
                usersResult = users.OrderByDescending(s => s.Company.Name);
                break;

            default:
                usersResult = users.OrderBy(s => s.Name);
                break;
            }

            IndexSortViewModel viewModel = new IndexSortViewModel
            {
                Users         = await usersResult.AsNoTracking().ToListAsync(),
                SortViewModel = new SortViewModel(sortOrder)
            };

            return(View(viewModel));
        }
        public async Task <IActionResult> AdminIndex(int?category, string name, int page = 1, SortState sortOrder = SortState.NameAsc)
        {
            //IEnumerable<ProductViewModel> model = _context.Set<Product>().ToList().Select(p => new ProductViewModel
            //{
            //    Id = p.Id,
            //    Name = p.Name,
            //    Description = p.Description,
            //    Price = p.Price,
            //    Date = p.Date,
            //    CategoryId = p.CategoryId,
            //    ImageProd = p.ImageProd ?? Consts.DefaultImageProd,
            //    Category = p.Category,
            //    CategoryName = _context.Set<Category>().SingleOrDefault(c => c.Id == p.CategoryId).Name,
            //});

            int pageSize = 6;

            //Filtration
            IQueryable <Product> products = _context.Product.Include(x => x.Category);

            if (category != null && category != 0)
            {
                products = products.Where(p => p.CategoryId == category);
            }
            if (!String.IsNullOrEmpty(name))
            {
                products = products.Where(p => p.Name.Contains(name));
            }

            //Sorting
            switch (sortOrder)
            {
            case SortState.NameDesc:
                products = products.OrderByDescending(s => s.Name);
                break;

            case SortState.PriceAsc:
                products = products.OrderBy(s => s.Price);
                break;

            case SortState.PriceDesc:
                products = products.OrderByDescending(s => s.Price);
                break;

            case SortState.CategoryAsc:
                products = products.OrderBy(s => s.Category.Name);
                break;

            case SortState.CategoryDesc:
                products = products.OrderByDescending(s => s.Category.Name);
                break;

            default:
                products = products.OrderBy(s => s.Name);
                break;
            }

            //Paging
            var count = await products.CountAsync();

            var items = await products.Skip((page - 1) *pageSize).Take(pageSize).ToListAsync();

            IndexSortViewModel viewModel = new IndexSortViewModel
            {
                PageViewModel   = new PageViewModel(count, page, pageSize),
                SortViewModel   = new SortViewModel(sortOrder),
                FilterViewModel = new FilterViewModel(_context.Category.ToList(), category, name),
                Products        = items
            };


            return(View(viewModel));
        }