Exemple #1
0
        // GET: Souvenirs
        public async Task <IActionResult> Index(
            string sortOrder,
            string searchString,
            string currentFilter,
            int?page)
        {
            //week3 create sorter
            ViewData["NameSortParm"]     = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            ViewData["PriceSortParm"]    = sortOrder == "price" ? "price_desc" : "price";
            ViewData["CategorySortParm"] = String.IsNullOrEmpty(sortOrder) ? "category" : "";
            ViewData["SupplierSortParm"] = String.IsNullOrEmpty(sortOrder) ? "supplier" : "";

            //week3 add paging
            ViewData["CurrentSort"] = sortOrder;

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            //week3 add search
            ViewData["CurrentFilter"] = searchString;
            var applicationDbContext = _context.Souvenirs.Include(s => s.Category).Include(s => s.Supplier);
            var souvenirs            = from s in applicationDbContext
                                       select s;

            if (!String.IsNullOrEmpty(searchString))
            {
                souvenirs = souvenirs.Where(s => s.Name.Contains(searchString) ||
                                            s.Description.Contains(searchString));
            }
            switch (sortOrder)
            {
            case "name_desc":
                souvenirs = souvenirs.OrderByDescending(s => s.Name);
                break;

            case "price":
                souvenirs = souvenirs.OrderBy(s => s.Price);
                break;

            case "price_desc":
                souvenirs = souvenirs.OrderByDescending(s => s.Price);
                break;

            case "category":
                souvenirs = souvenirs.OrderBy(s => s.Category);
                break;

            case "supplier":
                souvenirs = souvenirs.OrderBy(s => s.Supplier);
                break;

            default:
                souvenirs = souvenirs.OrderBy(s => s.Name);
                break;
            }
            int pageSize = 5;

            return(View(await PaginatedList <Souvenir> .CreatAsync(souvenirs.AsNoTracking(), page ?? 1, pageSize)));
        }
        // GET: Products
        public async Task <IActionResult> Index(
            string sortOrder,
            string searchString,
            string minPrice,
            string maxPrice,
            int?page,
            int?categoryId)
        {
            ViewData["SortParm"]      = sortOrder;
            ViewData["NameSortParm"]  = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            ViewData["PriceSortParm"] = sortOrder == "price" ? "price_desc" : "price";
            ViewData["SearchFilter"]  = searchString;
            ViewData["MinPrice"]      = minPrice;
            ViewData["MaxPrice"]      = maxPrice;

            var applicationDbContext = _context.Souvenirs;
            var souvenirs            = from s in applicationDbContext
                                       select s;

            if (!String.IsNullOrEmpty(searchString))
            {
                souvenirs = souvenirs.Where(s => s.Name.Contains(searchString) ||
                                            s.Description.Contains(searchString) ||
                                            s.Category.Description.Contains(searchString) ||
                                            s.Category.Name.Contains(searchString));
            }

            if (!(String.IsNullOrEmpty(minPrice)))
            {
                souvenirs = souvenirs.Where(s => s.Price >= Convert.ToDecimal(minPrice));
            }

            if (!(String.IsNullOrEmpty(maxPrice)))
            {
                souvenirs = souvenirs.Where(s => s.Price <= Convert.ToDecimal(maxPrice));
            }

            switch (sortOrder)
            {
            case "name_desc":
                souvenirs = souvenirs.OrderByDescending(s => s.Name);
                break;

            case "price":
                souvenirs = souvenirs.OrderBy(s => s.Price);
                break;

            case "price_desc":
                souvenirs = souvenirs.OrderByDescending(s => s.Price);
                break;

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

            int pageSize = 8;

            if (categoryId != null)
            {
                souvenirs = souvenirs.Where(c => c.CategoryID == categoryId);
            }
            var categories = _context.Categories.ToList();

            ViewData["Categories"] = categories;
            ViewData["categoryId"] = categoryId;

            return(View(await PaginatedList <Souvenir> .CreatAsync(souvenirs.AsNoTracking(), page ?? 1, pageSize)));
        }