예제 #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["TitleSortParm"]         = string.IsNullOrEmpty(sortOrder) ? "title_desc" : "";
            ViewData["AuthorSortParm"]        = sortOrder == "Author" ? "author_desc" : "Author";
            ViewData["PublishedSortParm"]     = sortOrder == "Published" ? "published_desc" : "Published";
            ViewData["PublishedDateSortParm"] = sortOrder == "PublishedDate" ? "publishedDate_desc" : "PublishedDate";

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

            ViewData["CurrentFilter"] = searchString;

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

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

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

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

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

            case "author_desc":
                orderBy = q => q.OrderByDescending(c => c.Author.UserName);
                break;

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

            case "published_desc":
                orderBy = q => q.OrderByDescending(c => c.Published);
                break;

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

            case "publishedDate_desc":
                orderBy = q => q.OrderByDescending(c => c.PublishedDate);
                break;

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

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

            return(View(posts));
        }