private async Task <QuestionHomeViewModel> GetQuestionsPaginated(int?PageIndex, int?TotalItems, string Query = null)
        {
            PageIndex = PageIndex == 0 ? 1 : PageIndex;
            if (TotalItems > 0)
            {
                PageIndex = (TotalItems / NUMBER_OF_ITEMS_PER_REQUEST) + 1;
            }

            QuestionHomeViewModel viewModel = new QuestionHomeViewModel();
            IQueryable <Question> questions = null;

            if (Query == null)
            {
                questions = _context.Questions.Include(x => x.Answers).OrderByDescending(x => x.DateCreated).AsNoTracking();
            }
            else
            {
                var words = Query.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                questions = _context.Questions
                            .Include(x => x.Answers)
                            .OrderByDescending(x => x.DateCreated)
                            .WhereAll_AND(words.Select(w => (Expression <Func <Question, bool> >)(x => EF.Functions.Like(x.Text, "%" + w + "%"))).ToArray())
                            .AsNoTracking();
            }

            viewModel.Questions = await PaginatedList <Question> .CreateAsync(questions, PageIndex ?? 1, NUMBER_OF_ITEMS_PER_REQUEST);

            return(viewModel);
        }
        public async Task <IActionResult> GetPartialView_QuestionsPaginated(bool ShowNavbarPagination, int?PageIndex, int?TotalItems, string Query = null)
        {
            try
            {
                QuestionHomeViewModel viewModel = await GetQuestionsPaginated(PageIndex, TotalItems, Query);

                viewModel.ShowNavbarPagination = ShowNavbarPagination;
                var viewHtmlPaginationQuestions = viewModel.Questions.Count > 0 ? _viewRenderService.RenderToStringAsync("_PaginationQuestions", viewModel).Result : "";
                int totalNumberQuestions        = await GetTotalNumberQuestions();

                return(new JsonResult(new { Response = "true", ViewPaginationQuestions = viewHtmlPaginationQuestions, TotalNumberQuestions = totalNumberQuestions }));
            }
            catch (Exception ex)
            {
                return(new JsonResult(new { Response = "false", Error = ex.Message }));
            }
        }
예제 #3
0
        public ActionResult Search(string searchString, int page = 0)
        {
            int             skip      = page * Constant.Question.Integer.PagingDefaultTake;
            List <Question> questions = qService.SearchQuestion(searchString.Trim(), skip);

            ICollection <QuestionPreviewViewModel> questionPreviewVMs =
                questions.Select(Mapper.Map <Question, QuestionPreviewViewModel>) // Using Mapper with Collection
                .ToList();

            for (int i = 0; i < questionPreviewVMs.Count; i++)
            {
                questionPreviewVMs.ElementAt(i).UserInfo.CreateMainPostDate = questionPreviewVMs.ElementAt(i).CreatedDate;
                if (Request.IsAuthenticated)
                {
                    questionPreviewVMs.ElementAt(i).Bookmarked =
                        questionPreviewVMs.ElementAt(i).UserId != User.Identity.GetUserId <int>() &&
                        questions.ElementAt(i).BookmarkUsers
                        .Where(u => u.Id == User.Identity.GetUserId <int>()).Count() > 0;
                }
            }
            if (page == 0)
            {
                QuestionHomeViewModel questionHomeVM = new QuestionHomeViewModel();
                questionHomeVM.Name = "Có " + qService.CountSearchResult(searchString)
                                      + " Kết Quả Tìm Kiếm Cho \"" + searchString + "\"";
                ViewBag.Tab          = Constant.Question.String.SearchTab;
                ViewBag.System       = Constant.String.QuestionSystem;
                ViewBag.TabParam     = searchString;
                questionHomeVM.Items = questionPreviewVMs;
                var cookie = new HttpCookie("returnUrl", Request.Url.AbsolutePath + Request.Url.Query);
                cookie.Expires = DateTime.Now.AddMinutes(5);
                Response.Cookies.Add(cookie);
                return(View("Views/QuestionHomeView", questionHomeVM));
            }
            else
            {
                return(PartialView("Partials/_QuestionListPartialView", questionPreviewVMs));
            }
        }