예제 #1
0
        public ActionResult Index(string searchText, int?page)
        {
            int pageNumber = (page ?? 1);

            if (String.IsNullOrEmpty(searchText))
            {
                var allNews = _repository.FindAllInclude("NewsCategory", "User");
                var result  = allNews.Where(n => n.Status == "Approved").OrderByDescending(n => n.ModifiedOn)
                              .Skip(pageSize * (pageNumber - 1)).Take(pageSize);

                if (result.Count() == 0)
                {
                    return(HttpNotFound());
                }

                ViewBag.Pages = (int)allNews.Where(n => n.Status == "Approved").Count() / pageSize + 1;
                if (allNews.Where(n => n.Status == "Approved").Count() % pageSize == 0)
                {
                    ViewBag.Pages = ViewBag.Pages - 1;
                }

                var newsVM = new NewsVM
                {
                    News = result
                };

                return(View(newsVM));
            }

            else
            {
                var selectedNews = _repository.FindAllInclude("NewsCategory", "User");
                var result       = selectedNews.Where(n => n.Status == "Approved" && (n.Title.ToLower()
                                                                                      .Contains(searchText.ToLower()) || n.Content.ToLower().Contains(searchText.ToLower())))
                                   .OrderByDescending(n => n.ModifiedOn)
                                   .Skip(pageSize * (pageNumber - 1))
                                   .Take(pageSize);

                if (result.Count() == 0)
                {
                    return(HttpNotFound());
                }

                ViewBag.Pages = (int)selectedNews.Where(n => n.Status == "Approved").Count() / pageSize + 1;
                if (selectedNews.Where(n => n.Status == "Approved").Count() % pageSize == 0)
                {
                    ViewBag.Pages = ViewBag.Pages - 1;
                }

                var newsVM = new NewsVM
                {
                    News = result
                };

                return(View(newsVM));
            }
        }