//If you go to, /blogs/, it should display all of the Blogs (My Personal Blog, My Programming Blog) //in the system public ActionResult Index() { BlogsListingViewModel model = new BlogsListingViewModel(); model.Blogs = new List <BlogsListingViewModel.BlogItem>(); //Initialize array so it is not null because we need to add to it below string sql = @" SELECT A.BlogId, A.BlogTitle, A.CreatedDate , (SELECT COUNT(*) FROM BlogPosts B WHERE B.BlogId = A.BlogId) AS PostCount FROM Blogs A "; using (SqlConnection conn = new SqlConnection(CommonUtility.GetMainConnectionstring())) { using (SqlCommand comm = new SqlCommand(sql, conn)) { conn.Open(); SqlDataReader reader = comm.ExecuteReader(); while (reader.Read()) { BlogsListingViewModel.BlogItem blog = new BlogsListingViewModel.BlogItem(); blog.BlogId = reader.GetInt32(reader.GetOrdinal("BlogId")); blog.BlogTitle = reader.GetString(reader.GetOrdinal("BlogTitle")); blog.CreatedDate = reader.GetDateTime(reader.GetOrdinal("CreatedDate")); blog.PostCount = reader.GetInt32(reader.GetOrdinal("PostCount")); model.Blogs.Add(blog); } } } //Okay, we got all the information, lets return the data structure we loaded back to the view for //html display return(View(model)); }
public ActionResult Index(BlogListingPage currentPage, int page = 0) { // var listBlogDetail = new List<BlogDetailPage>(); var md = new BlogsListingViewModel(currentPage); var count = 0; var listBlogDetail = _contentLoader.GetChildren <BlogDetailPage>(currentPage.ContentLink).Where(x => DateTime.Compare(x.StartPublish, DateTime.Now) < 0 && DateTime.Compare(x.StopPublish, DateTime.Now) > 0).ToList(); if (!string.IsNullOrEmpty(Request.Unvalidated.QueryString["q"])) { var listBlogSearch = GetBlogSearch(currentPage, Request.Unvalidated.QueryString["q"]); md.ListBlogDetail = listBlogSearch; count = listBlogSearch.Count; } else { var listBlogAfterFilter = listBlogDetail; if (!string.IsNullOrEmpty(Request.Unvalidated.QueryString["cat"])) { listBlogAfterFilter = Request.Unvalidated.QueryString["cat"].ToString() == "Uncategorized" ? listBlogDetail.Where(x => string.IsNullOrEmpty(x.BlogCategory)).ToList() : listBlogDetail.Where(x => x.BlogCategory == Request.QueryString["cat"].ToString()).ToList(); } if (!string.IsNullOrEmpty(Request.Unvalidated.QueryString["author"])) { listBlogAfterFilter = listBlogDetail.Where(x => x.AuthorName == Request.Unvalidated.QueryString["author"].ToString()).ToList(); } if (!string.IsNullOrEmpty(Request.Unvalidated.QueryString["tag"])) { listBlogAfterFilter = listBlogDetail.Where(x => x.Tags != null && x.Tags.Contains(Request.QueryString["tag"].ToString())).ToList(); } ViewBag.ItemsPerPage = currentPage.ItemsPerPage; md.ListBlogDetail = listBlogAfterFilter; count = listBlogAfterFilter.Count; } ViewBag.ItemsPerPage = currentPage.ItemsPerPage; md.IsAjaxRequestLastPage = (page + 1) * currentPage.ItemsPerPage >= count; md.ListBlogDetail = md.ListBlogDetail.Skip(page * currentPage.ItemsPerPage) .Take(currentPage.ItemsPerPage).ToList(); if (HttpContext.Request.IsAjaxRequest() && page > 0) { if (md.ListBlogDetail.Any()) { return(PartialView("_BlogItem", md)); } return(Content("StringForEndingListingPage")); } // md.ListBlogDetail = listBlogAfterFilter; var listCategory = listBlogDetail.Select(x => x.BlogCategory).Distinct().ToList(); // var listCategory = CategoryHelper.GetCategories("Blog Category").Select(x => x.Name); var listBlogCategory = (from categoryName in listCategory let blogCount = listBlogDetail.Count(x => x.BlogCategory == categoryName) select new BlogCategory() { BlogCount = blogCount, Category = !string.IsNullOrEmpty(categoryName) ? categoryName : "Uncategorized" }).Where(x => x.BlogCount > 0) .OrderByDescending(x => x.BlogCount) .ToList(); md.ListBlogCategory = listBlogCategory; var listTagCategory = new List <string>(); // 1,2,3 | 3,2,4 | 3,4,5 foreach (var tag in listBlogDetail.Select(x => x.Tags).ToList()) { if (!string.IsNullOrEmpty(tag)) { listTagCategory.AddRange(tag.Split(',')); } } listTagCategory = listTagCategory.Distinct().ToList(); md.ListTags = (from tagName in listTagCategory let blogCount = listBlogDetail.Count(x => !string.IsNullOrEmpty(x.Tags) && x.Tags.Contains(tagName)) select new Models.ViewModels.Tags() { BlogCount = blogCount, TagName = tagName }).OrderByDescending(x => x.BlogCount).ToList(); var listAuthor = listBlogDetail.Where(x => !x.IsDeleted).Select(x => x.AuthorName).Distinct().ToList(); md.ListAuthors = (from authorName in listAuthor let blogCount = listBlogDetail.Count(x => x.AuthorName == authorName) select new Authors() { BlogCount = blogCount, AuthorName = authorName }).OrderByDescending(x => x.BlogCount).ToList(); // md.ListTags = listTags; var listJob = new List <JobPage>(); if (currentPage.JobRoot != null) { CareerPage carrerPage; if (_contentLoader.TryGet(currentPage.JobRoot, out carrerPage)) { foreach (var contentAreaItem in carrerPage.JobOpenings.Items) { JobPage jobPage; if (_contentLoader.TryGet(contentAreaItem.ContentLink, out jobPage)) { listJob.Add(jobPage); } } } } // listJob = _contentLoader.GetChildren<JobPage>(currentPage.JobRoot).OrderBy(x => x.Created).Take(3).ToList(); md.ListJobPage = listJob.Take(3).ToList(); return(PartialView(md)); }