/// <summary> /// もっと見る取得 /// </summary> /// <param name="currentCount">現在表示しているレコード件数</param> /// <returns>Json形式のActionResult</returns> public ActionResult GetMoreArticles(int currentCount) { MyPageArticleViewModel viewModel = new MyPageArticleViewModel(); int ajaxInitialSize = 3; //DBから取得する処理 SetLines(viewModel, currentCount, currentCount == 0?ajaxInitialSize:MyPageArticleViewModel.INITIAL_PAGE_SIZE); return Json(viewModel, JsonRequestBehavior.AllowGet); }
/// <summary> /// 投稿をDBから取得する処理 /// </summary> /// <param name="viewModel">ビューモデル</param> /// <param name="skipCount">スキップする要素数</param> /// <param name="takeCount">返す要素数</param> private void SetLines(MyPageArticleViewModel viewModel, int skipCount, int takeCount) { try { long memberId = -1; object currentUser = Session["CurrentUser"]; if (currentUser != null) memberId = Convert.ToInt64(currentUser.ToString()); if (memberId > 0) { //投稿(Contribution)テーブルを検索する //(の投稿日時(ContributeDate)から該当月のもの) <=HTMLデザインに準じ、廃止 //同テーブルのタイトル(Title)を表示 var contributions = from c in com.Contribution where c.MemberId == memberId orderby c.ContributeDate descending, c.ContributeId select new ContributionForMyPage { ContributeId = c.ContributeId, MemberId = c.MemberId, Title = c.Title, Body = c.Body, ContributedPicture = c.ContributedPicture, ContributeDate = c.ContributeDate, ModifiedDate = c.ModifiedDate }; viewModel.TotalCount = contributions.Count(); //「投稿」テーブルの投稿ID で、「引用トピック」テーブルを読んで、トピックID を取得する。 //この トピックID で、「トピックマスタ」を読んで、「分類種別」が 4の時のトピック名を表示 var topicTitles = from c in com.Contribution from q in com.QuotTopic.Where(x => x.ContributeID == c.ContributeId).DefaultIfEmpty() from t in com.TopicMaster.Where(x => x.TopicID == q.TopicID).DefaultIfEmpty() where c.MemberId == memberId && t.ClassificationType == 4 select new ContributionForMyPage { ContributeId = c.ContributeId, TopicTitles = t.TopicName, }; //収集したトピックタイトルをビューで投稿に付与 //表示分読み込む viewModel.Contributions = new List<ContributionForMyPage> { }; bool topicFg = false; IEnumerable<ContributionForMyPage> list = contributions.Skip(skipCount).Take(takeCount); foreach (ContributionForMyPage c in list) { foreach (ContributionForMyPage t in topicTitles) { if (t.ContributeId == c.ContributeId) { topicFg = true; c.TopicTitles += (c.TopicTitles == null ? "" : ",") + t.TopicTitles; } else { if (topicFg) { break; } } } ((List<ContributionForMyPage>)viewModel.Contributions).Add(c); } } } catch (Exception ex) { throw ex; } }