Beispiel #1
0
        // GET: Products
        public async Task <IActionResult> Index(string searchString, int?pageNumber, string currentFilter,
                                                string sortOrder, int pageSize, string setPageSize)
        {
            ViewData["NameSortParm"]     = sortOrder == "name" ? "name_desc" : "name";
            ViewData["QuantitySortParm"] = sortOrder == "quantity" ? "quantity_desc" : "quantity";
            ViewData["PriceSortParm"]    = sortOrder == "price" ? "price_desc" : "price";
            ViewData["CurrentFilter"]    = searchString;
            if (HttpContext.Session.GetInt32("userId") != null)
            {
                var products = from p in _context.Product
                               select p;

                if (sortOrder != null)
                {
                    HttpContext.Session.SetString("sort", sortOrder);
                }
                sortOrder = HttpContext.Session.GetString("sort");

                switch (sortOrder)
                {
                case "name":
                    products = products.OrderBy(p => p.Name);
                    break;

                case "name_desc":
                    products = products.OrderByDescending(p => p.Name);
                    break;

                case "quantity":
                    products = products.OrderBy(p => p.Quantity);
                    break;

                case "quantity_desc":
                    products = products.OrderByDescending(p => p.Quantity);
                    break;

                case "price":
                    products = products.OrderBy(p => p.Price);
                    break;

                case "price_desc":
                    products = products.OrderByDescending(p => p.Price);
                    break;

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

                switch (setPageSize)
                {
                case "set10":
                    HttpContext.Session.SetInt32("pagesize", 10);
                    break;

                case "set20":
                    HttpContext.Session.SetInt32("pagesize", 20);
                    break;

                case "set100":
                    HttpContext.Session.SetInt32("pagesize", 100);
                    break;

                case "setAll":
                    HttpContext.Session.SetInt32("pagesize", products.Count());
                    break;
                }

                if (searchString != null)
                {
                    pageNumber = 1;
                }
                else
                {
                    searchString = currentFilter;
                }
                if (!String.IsNullOrEmpty(searchString))
                {
                    products = products.Where(p => p.Name.ToUpper().Contains(searchString.ToUpper()));
                }


                if (HttpContext.Session.GetInt32("pagesize").HasValue)
                {
                    pageSize = HttpContext.Session.GetInt32("pagesize").GetValueOrDefault();
                }
                else
                {
                    pageSize = 10;
                }

                return(View(await PaginatedList <Product> .CreateAsyc(products.AsNoTracking(), pageNumber ?? 1, pageSize)));
            }
            else
            {
                return(RedirectToAction("Login", "Login"));
            }
        }