/// <summary>
        /// 1.0 HttpGet评论分页 采用递归的方法直接在后台拼接好Ajax评论内容及分页Html,再将json返回到前台
        /// </summary>
        /// <param name="id">博文id</param>
        /// <returns></returns>
        public JsonResult WrapComment(int id)
        {
            int pageIndex = Request["pageIndex"] == null ? 1 : int.Parse(Request["pageIndex"]);
            int pageSize  = Request["pageSize"] == null ? 4 : int.Parse(Request["pageSize"]);

            //1.1 选出当前博文的该页内的一级评论
            List <Comment> topCmtList = commentService.GetPagedList(pageIndex, pageSize, c => c.CmtArtId == id && c.Status == 1 && c.ParentId == 0, c => c.SubTime, true);

            //1.2 创建评论框区域的大容器
            StringBuilder cmtHtmlStr = new StringBuilder("<ol class=\"commentlist\">");

            //1.3 递归获取各级评论,并追加到cmtHtmlString中
            PackCmt(topCmtList, cmtHtmlStr);

            //1.4 合上评论框区域的大容器,评论区域HTML拼接完毕
            cmtHtmlStr.Append("</ol>");

            //1.5 评论总Html
            string cmtHtmlString = cmtHtmlStr.ToString();

            //1.6 评论分页Html
            string cmtPagerString = string.Empty;

            //1.7 该篇博文的所有评论(连带回复)
            int totalCount = commentService.GetDataListBy(c => c.CmtArtId == id && c.Status == 1 && c.ParentId == 0).Count;//分页插件用

            if (totalCount > 0)
            {
                cmtPagerString = PagerHelper.GeneratePagerString(pageIndex, pageSize, totalCount);
                cmtPagerString = "<div class='pagination'>" + "<ul>" + cmtPagerString + "</ul>" + "</div>";
            }

            //1.8 构造json数据
            JsonModel jsonData = new JsonModel()
            {
                CoreData   = cmtHtmlString,
                Status     = 1,
                Message    = "成功",
                GotoUrl    = HttpContext.Request.Url.AbsoluteUri,
                PageNavStr = cmtPagerString
            };

            return(Json(jsonData, JsonRequestBehavior.AllowGet));
        }
Exemple #2
0
        /// <summary>
        /// 2.0 Index页-Ajax获取首页博文列表;显示5条
        /// </summary>
        /// <param name="id">博文类别(UrlParameter.optional)</param>
        /// <returns>JSON</returns>
        public ActionResult WrapArtList(int id)
        {
            int pageIndex = Request["pageIndex"] == null ? 1 : int.Parse(Request["pageIndex"]);
            int pageSize  = Request["pageSize"] == null ? 8 : int.Parse(Request["pageSize"]);

            List <Article> articleList = new List <Article>();
            int            totalCount  = 0;

            if (id == 0) //取所有博文,对应于“首页”<Home/Index/0>
            {
                articleList = articleService.GetPagedList(pageIndex, pageSize, a => a.Status == 1, a => a.SubTime, true);

                totalCount = articleService.GetDataListBy(a => a.Status == 1).Count; //总条数
            }
            else  //取响应板块的博文,对应于“导航栏”<Home/Index/6>
            {
                articleList = articleService.GetPagedList(pageIndex, pageSize, a => a.CategoryId == id && a.Status == 1, a => a.SubTime, true);

                totalCount = articleService.GetDataListBy(a => a.CategoryId == id && a.Status == 1).Count; //总条数
            }

            //视图模型需要类别名称和博文基本信息
            List <ArticleViewModel> articleViewList = new List <ArticleViewModel>();

            foreach (Article item in articleList)
            {
                //1.0 获取博文类别名
                string categoryName = item.Category.Name;
                //2.0 获取博文关键词  关键词可为空
                string[]      keywords = null;
                List <string> keylist  = new List <string>();
                if (!string.IsNullOrEmpty(item.Keywords))
                {
                    //关键词规则约定:以空格分开
                    keywords = item.Keywords.Split(' ');

                    foreach (var word in keywords)
                    {
                        if (word != " ")
                        {
                            keylist.Add(word);
                        }
                    }
                }
                //取前五个关键词
                keywords = keylist.Take(5).ToArray();

                //3.0 获取评论总数:
                int commentCount = commentService.GetDataListBy(c => c.CmtArtId == item.Id && c.Status == 1).Count;

                //4.0 构造视图模型
                ArticleViewModel artViewModel = new ArticleViewModel()
                {
                    Id           = item.Id,
                    Title        = item.Title,
                    SubTime      = item.SubTime.ToShortDateString(),
                    CategoryName = categoryName,
                    ViewCount    = item.ViewCount,
                    CommentCount = commentCount,
                    Digg         = item.Digg,
                    Contents     = StringHelper.StringCut(item.ContentsRaw, 600), //ContentsRaw为不包含html标签的文本
                    Keywords     = keywords                                       //可能为空,为空则View中不显示
                };

                articleViewList.Add(artViewModel);
            }
            //5.0 构造分页html-json
            string PagerNavString = PagerHelper.GeneratePagerString(pageIndex, pageSize, totalCount);

            JsonModel jsonData = new JsonModel()
            {
                CoreData   = articleViewList,
                Status     = 1,
                Message    = "成功",
                GotoUrl    = HttpContext.Request.Url.AbsolutePath,
                PageNavStr = PagerNavString
            };

            return(Json(jsonData, JsonRequestBehavior.AllowGet));//Json方法默认只接受post请求
        }