Exemplo n.º 1
0
        // GET: Comments
        public async Task <ActionResult> Index(string sortOrder, string currentFilter, string searchString, int?page)
        {
            ViewBag.CurrentSort  = sortOrder;
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }
            ViewBag.CurrentFilter = searchString;
            IEnumerable <Comment> entityList = null;

            if (await UserRoleManage.IsUserIdInRole(User.Identity.GetUserId(), "Admin"))
            {
                entityList = await _commentDal.QueryAllAsync();
            }
            else
            {
                entityList = await _commentDal.QueryByFunAsync(t => t.Creater == User.Identity.Name || t.LastModifier == User.Identity.Name);
            }
            if (entityList.Any())
            {
                if (!String.IsNullOrEmpty(searchString))
                {
                    entityList = entityList.Where(s => (s.Content != null && s.Content.Contains(searchString)) ||
                                                  (s.UserName != null && s.UserName.Contains(searchString)) ||
                                                  (s.Phone != null && s.Phone.Contains(searchString)));
                }
                switch (sortOrder)
                {
                case "name_desc":
                    entityList = entityList.OrderByDescending(s => s.UserName);
                    break;

                case "Date":
                    entityList = entityList.OrderBy(s => s.LastModifyDate);
                    break;

                case "date_desc":
                    entityList = entityList.OrderByDescending(s => s.LastModifyDate);
                    break;

                default:
                    entityList = entityList.OrderBy(s => s.UserName);
                    break;
                }
            }
            int pageSize   = 20;
            int pageNumber = (page ?? 1);

            return(View(entityList.ToPagedList(pageNumber, pageSize)));
        }