public IActionResult Index(string text, string tagName, int?pageSize, int?pageNumber) { List <Tag> tagListFilter = _TagCollection.GetTagList(); tagListFilter = tagListFilter.DistinctBy(x => x.Name).ToList(); SelectList tagSelectListFilter = new SelectList(tagListFilter, "Name", "Name", tagName ?? ""); SelectList pageSizeSelectList = new SelectList(new[] { new { ID = "5", Name = "5" }, new { ID = "10", Name = "10" }, new { ID = "15", Name = "15" }, new { ID = "25", Name = "25" }, new { ID = "50", Name = "50" }, new { ID = "100", Name = "100" }, }, "ID", "Name", pageSize ?? 5); List <Post> postList = _PostCollection.GetPostListWithFilters(text, tagName); List <PostViewModel> postViewModelList = _Mapper.Map <List <PostViewModel> >(postList); foreach (PostViewModel postViewModel in postViewModelList) { List <Tag> tagList = _TagCollection.GetTagListFromPostId(postViewModel.Post_Id); postViewModel.TagList = _Mapper.Map <List <TagViewModel> >(tagList); } postViewModelList = postViewModelList.OrderByDescending(x => x.Posted).ToList(); PaginatedList <PostViewModel> postViewModelPaginatedList = PaginatedList <PostViewModel> .CreateList(postViewModelList, pageSize ?? 5, pageNumber ?? 1); string metaTags = ""; foreach (Tag tag in tagListFilter) { metaTags = $"{metaTags} {tag.Name}"; } ViewData["meta"] = $"Blog {metaTags}"; ViewData["title"] = "Blog"; ViewData["body"] = "body-blog"; ViewData["current_filter_text"] = text; ViewData["prev_disabled"] = !postViewModelPaginatedList.HasPreviousPage ? "disabled" : ""; ViewData["next_disabled"] = !postViewModelPaginatedList.HasNextPage ? "disabled" : ""; ViewBag.TagSelectListFilter = tagSelectListFilter; ViewBag.PageSizeSelectList = pageSizeSelectList; return(View(postViewModelPaginatedList)); }
public async Task OnGetAsync(string sortOrder, string currentFilter, string searchString, int?pageIndex) { CurrentSort = sortOrder; NameSort = String.IsNullOrEmpty(sortOrder) ? "name_desc" : ""; CodeSort = sortOrder == "Code" ? "code_desc" : "Code"; var cit = await _context.Cit.ToListAsync(); if (!string.IsNullOrEmpty(searchString)) { pageIndex = 1; cit = cit.Where(s => s.Name.Contains(searchString) || s.Code.Contains(searchString)).ToList(); } else { searchString = currentFilter; } CurrentFilter = searchString; switch (sortOrder) { case "name_desc": cit = cit.ToList().OrderByDescending(s => s.Name).ToList(); break; case "Code": cit = cit.ToList().OrderBy(s => s.Code).ToList(); break; case "code_desc": cit = cit.ToList().OrderByDescending(s => s.Code).ToList(); break; default: cit = cit.ToList().OrderBy(s => s.Name).ToList(); break; } this.Cit = PaginatedList <Cit> .CreateList(cit, pageIndex ?? 1, 10); this.TotalPages = this.Cit.TotalPages; }