public ActionResult Search(string q) { var pageIndex = Convert.ToInt32(Request["pageIndex"] ?? "1"); var pageSize = 10; var totalCount = 0; Stopwatch watch = new Stopwatch(); watch.Start(); // 页码 ViewBag.Index = pageIndex; if (string.IsNullOrWhiteSpace(q)) { ViewBag.Count = 0; ViewBag.Time = 0; ViewBag.Page = 1; return(View()); } List <SearchArticleResult> list = null; try { list = PanGuLuceneHelper.Instance.Search(q, pageIndex, pageSize, out totalCount); } catch (Exception e) { //return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, e.Message); } // 结果数 ViewBag.Count = totalCount; // 搜索词 ViewBag.Query = q; // 总页数 ViewBag.Page = PagerHelper.GetTotalPage(pageSize, totalCount); watch.Stop(); // 搜索所需时间 ViewBag.Time = watch.ElapsedMilliseconds; return(View(list)); }
public ActionResult Blogs(string author, int?page = 1) { var user = userService.LoadEntity(u => u.Login_Name.Equals(author, StringComparison.CurrentCultureIgnoreCase)); if (user != null) { var pageIndex = page.HasValue ? (page.Value <= 0 ? 1 : page.Value) : 1; var pageSize = GlobalConfig.PageSize; ViewBag.UserInfo = user; // 文章档案 //ViewBag.Archives = articleService.GetArchivesByUserId<Archives>(user.User_Id); //// 文章分类 //ViewBag.Categories = articleService.GetCustomCategoriesByUserId<CustomCategories>(user.User_Id); // 上一页、下一页 var totalCount = articleService.GetArticlesTotalCount(user.User_Id); var totalPage = PagerHelper.GetTotalPage(GlobalConfig.PageSize, totalCount);// totalCount % PageSize == 0 ? totalCount / PageSize : (totalCount + PageSize) / PageSize; if (pageIndex > 1 && pageIndex <= totalPage) { ViewBag.HasPre = true; ViewBag.Pre = pageIndex - 1; } if (pageIndex < totalPage) { ViewBag.HasNext = true; ViewBag.Next = pageIndex + 1; } if (pageIndex > totalPage) { pageIndex = 1; } var model = LoadRightMenusData <BlogsModel>(user.User_Id); // TODO : 可以优化 // 置顶文章只在第一页(置顶文章数量最多20) if (pageIndex <= 1) { // 置顶文章 var topList = articleService.LoadEntities(a => a.Author_Id == user.User_Id && a.IsTop) .OrderByDescending(a => a.Creation_Time)?.ToList(); //ViewBag.TopList = topList; model.TopArticles = topList; // 其他文章数量 = 设定数量 - 置顶文章数量 pageSize -= topList.Count(); if (pageSize > 0) { // 按日期分组的文章 类型为 IEnumerable<IGrouping<DateTime, Article>> var articles = articleService.LoadEntities(a => a.Author_Id == user.User_Id && a.State == 3 && !a.IsTop) .OrderByDescending(a => a.Creation_Time) .Take(pageSize) .GroupBy(new Func <Article, DateTime>(a => { var date = Convert.ToDateTime(a.Creation_Time.ToShortDateString()); return(date); })); //return View(articles); model.GroupingArticles = articles; return(View(model)); } } // 其他非置顶文章(排序:置顶在前,跳过第一页) else { // 按日期分组的文章 类型为 IEnumerable<IGrouping<DateTime, Article>> var articles = articleService.LoadPageEntities(a => a.Author_Id == user.User_Id && a.State == 3, pageIndex, pageSize) .GroupBy(new Func <Article, DateTime>(a => { var date = Convert.ToDateTime(a.Creation_Time.ToShortDateString()); return(date); })); //return View(articles); model.GroupingArticles = articles; return(View(model)); } } throw new HttpException(404, "Not Found!"); }