예제 #1
0
        public ActionResult ListCategories(int?page, int?itemsPerPage, string sortOrder, string currentFilter, string searchString)
        {
            if (!isAdmin())
            {
                return(RedirectToAction("LogIn", "Main"));
            }
            ViewBag.CurrentSort      = sortOrder;
            ViewBag.IdSortParm       = String.IsNullOrEmpty(sortOrder) ? "id_desc" : "";
            ViewBag.CategorySortParm = sortOrder == "Cat" ? "cat_desc" : "Cat";


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

            ViewBag.CurrentFilter = searchString;
            List <Category> allCategories = _categoryBLL.getAll(null);

            switch (sortOrder)
            {
            case "id_desc":
                allCategories = allCategories.OrderByDescending(s => s.ID).ToList();
                break;

            case "cat_desc":
                allCategories = allCategories.OrderByDescending(s => s.name).ToList();
                break;

            case "Cat":
                allCategories = allCategories.OrderBy(s => s.name).ToList();
                break;

            default:
                allCategories = allCategories.OrderBy(s => s.ID).ToList();
                break;
            }

            List <CategoryInfo> list = new List <CategoryInfo>();

            foreach (var item in allCategories)
            {
                list.Add(
                    new CategoryInfo()
                {
                    id   = item.ID,
                    name = item.name
                });
            }

            return(View(list.ToPagedList(pageNumber: page ?? 1, pageSize: itemsPerPage ?? 15)));
        }