Exemplo n.º 1
0
        public ViewResult Index(string sortOrder, string currentFilter, string searchUser, int?page)
        {
            ViewBag.CurrentSort   = sortOrder;
            ViewBag.LoginSortParm = (string.IsNullOrEmpty(sortOrder) || sortOrder != "login_asc") ? "login_asc" : "login_desc";
            ViewBag.NameSortParm  = (string.IsNullOrEmpty(sortOrder) || sortOrder != "name_asc") ? "name_asc" : "name_desc";
            ViewBag.MailSortParm  = (string.IsNullOrEmpty(sortOrder) || sortOrder != "mail_asc") ? "mail_asc" : "mail_desc";
            ViewBag.IdSortParm    = (string.IsNullOrEmpty(sortOrder) || sortOrder != "id_desc") ? "id_desc" : "id_asc";

            if (searchUser != null)
            {
                page = 1;
            }
            else
            {
                searchUser = currentFilter;
            }

            ViewBag.CurrentFilter = searchUser;

            ListadeUsuarios = dbc.Select();
            var users = from u in ListadeUsuarios select u;

            if (!string.IsNullOrEmpty(searchUser))
            {
                users = users.Where(u => u.UserName.ToLowerInvariant().Contains(searchUser.ToLowerInvariant()) ||
                                    u.UserLogin.ToLowerInvariant().Contains(searchUser.ToLowerInvariant()) ||
                                    u.UserEmail.ToLowerInvariant().Contains(searchUser.ToLowerInvariant()));
            }

            switch (sortOrder)
            {
            case "login_asc":
                users = users.OrderBy(u => u.UserLogin);
                break;

            case "name_asc":
                users = users.OrderBy(u => u.UserName);
                break;

            case "mail_asc":
                users = users.OrderBy(u => u.UserEmail);
                break;

            case "login_desc":
                users = users.OrderByDescending(u => u.UserLogin);
                break;

            case "name_desc":
                users = users.OrderByDescending(u => u.UserName);
                break;

            case "mail_desc":
                users = users.OrderByDescending(u => u.UserEmail);
                break;

            case "id_desc":
                users = users.OrderByDescending(u => u.UserId);
                break;

            default:
                users = users.OrderBy(u => u.UserId);
                break;
            }

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

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