Esempio n. 1
0
        public ActionResult Index(string q, int?cid, int page = 1)
        {
            var pageSize = 10;


            IQueryable <Post> posts    = db.Posts;
            Category          category = null;

            if (q != null)
            {
                posts = posts.Where(x => x.Category.CategoryName.Contains(q) || x.Title.Contains(q) || x.Content.Contains(q) || x.Author.DisplayName.Contains(q));
            }

            if (cid != null && q == null)
            {
                category = db.Categories.Find(cid);

                if (category == null)
                {
                    return(HttpNotFound());
                }

                posts = posts.Where(x => x.CategoryId == cid);
            }


            var vm = new HomeIndexViewModel
            {
                Posts      = posts.OrderByDescending(x => x.CreationTime).ToPagedList(page, pageSize),
                Category   = category,
                SearchTerm = q,
                CategoryId = cid
            };

            return(View(vm));
        }
        public ActionResult Index(string q, int?cid, string slug, int page = 1)
        {
            var pageSize               = 10;
            IQueryable <Post> posts    = db.Posts;
            Category          category = null; //olurda cid e girmezse bunu atamış olucaz

            //search için
            if (q != null)
            {
                posts = posts.Where(x => x.Category.CategoryName.Contains(q) ||
                                    x.Title.Contains(q) ||
                                    x.Content.Contains(q)
                                    );
            }

            if (cid != null && q == null)  //cid varsa bunu koy
            {
                category = db.Categories.Find(cid);
                if (category == null)
                {
                    return(HttpNotFound());
                }

                posts = posts.Where(x => x.CategoryId == cid);
            }
            var vm = new HomeIndexViewModel
            {
                //Posts = db.Posts.OrderByDescending(x => x.CreationTime).ToList()
                Posts      = posts.OrderByDescending(x => x.CreationTime).ToPagedList(page, pageSize),
                Category   = category,
                SearchTerm = q,
                CategoryId = cid
            };

            return(View(vm));
        }