Ejemplo n.º 1
0
        public ActionResult ListPost(int?page, string category)
        {
            BlogEntities         db        = new BlogEntities();
            List <PostViewModel> viewModel = new List <PostViewModel>();
            string sql = "";

            if (category == null || category == "")
            {
                sql = string.Format(@"SELECT * FROM POST ORDER BY CREATED_DATE DESC");
            }
            else
            {
                List <TAG> tags = StaticCache.GetTags();
                if (tags.Any(c => c.NAME.ToUpper() == category.ToUpper()))
                {
                    sql = string.Format(@"SELECT * FROM POST WHERE UPPER(MAIN_TAG)='{0}' ORDER BY CREATED_DATE DESC ", category.ToUpper());
                }
                else
                {
                    sql = string.Format(@"SELECT * FROM POST ORDER BY CREATED_DATE DESC");
                }
            }

            viewModel = db.POSTs.SqlQuery(sql).Select(c => new PostViewModel {
                TITLE = c.TITLE, IMAGE_COVER = c.IMAGE_COVER, SLUG = c.SLUG, CREATED_DATE = c.CREATED_DATE, CREATED_USER = c.CREATED_USER, MAIN_TAG = c.MAIN_TAG, META_DESC = c.META_DESC
            }).ToList();

            int pageSize   = 5;
            int pageNumber = (page ?? 1);

            ViewBag.tag = category;
            return(View(viewModel.ToPagedList(pageNumber, pageSize)));
        }