コード例 #1
0
        public async Task <IActionResult> Index(string sortOrder, string currentFilter, string searchString, int?pageIndex = 1, int pageSize = 10)
        {
            ViewData["CurrentPageSize"]      = pageSize;
            ViewData["CurrentSort"]          = sortOrder;
            ViewData["NameSortParm"]         = string.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            ViewData["SlugSortParm"]         = sortOrder == "Slug" ? "slug_desc" : "Slug";
            ViewData["CreatedDateSortParm"]  = sortOrder == "CreatedDate" ? "createdDate_desc" : "CreatedDate";
            ViewData["ModifiedDateSortParm"] = sortOrder == "ModifiedDate" ? "modifiedDate_desc" : "ModifiedDate";

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

            ViewData["CurrentFilter"] = searchString;

            Expression <Func <Tag, bool> > filter = null;

            if (!string.IsNullOrEmpty(searchString))
            {
                filter = c => c.Name.Contains(searchString) || c.Slug.Contains(searchString);
            }

            Func <IQueryable <Tag>, IOrderedQueryable <Tag> > orderBy = null;

            switch (sortOrder)
            {
            case "name_desc":
                orderBy = q => q.OrderByDescending(c => c.Name);
                break;

            case "Slug":
                orderBy = q => q.OrderBy(c => c.Slug);
                break;

            case "slug_desc":
                orderBy = q => q.OrderByDescending(c => c.Slug);
                break;

            case "CreatedDate":
                orderBy = q => q.OrderBy(c => c.CreatedDate);
                break;

            case "createdDate_desc":
                orderBy = q => q.OrderByDescending(c => c.CreatedDate);
                break;

            case "ModifiedDate":
                orderBy = q => q.OrderBy(c => c.ModifiedDate);
                break;

            case "modifiedDate_desc":
                orderBy = q => q.OrderByDescending(c => c.ModifiedDate);
                break;

            default:
                orderBy = q => q.OrderBy(c => c.Name);
                break;
            }

            var tags = await _tagServices.GetAsync(filter : filter, orderBy : orderBy, pageIndex : pageIndex ?? 1, pageSize : pageSize);

            return(View(tags));
        }