예제 #1
0
        public async Task <ActionResult> Index()
        {
            ViewBag.searchParameter = "";

            int pageSize   = 10;
            int pageNumber = 1;

            //cannot do it like this due to EF6 sql statements mixing with C# code.
            //linq queries cannot recognize inner c# statements as they are being converted to sql statements
            //var users = userManager.Users.Select(user => UserViewModel.Create(user, roleManager));

            //.ToList() closes the SqlReader !! If not closed then must edit config to allow multiple readers to access the sql table
            //get admin role id
            string adminRoleId = (await roleManager.FindByNameAsync("admin")).Id;
            //exclude admin from the result
            var users = await userManager.Users.Where(user => user.Roles.FirstOrDefault().RoleId != adminRoleId).ToListAsync();

            var viewModelList = await UserViewModel.CreateFromQueryAsListAsync(users, roleManager);

            return(View(viewModelList.OrderBy(record => record.UserId).ToPagedList(pageNumber, pageSize)));
        }
예제 #2
0
        public async Task <ActionResult> UpdateIndex(string searchParameter, int?page, string sortColumn, string sortOrder)
        {
            int pageNumber = (page ?? 1);
            int pageSize   = 10;

            IEnumerable <UserViewModel>   viewModelList;
            IEnumerable <ApplicationUser> users;

            //filtering the query
            if (searchParameter != null && searchParameter != "")
            {
                //searching through the email, first name and last name
                ViewBag.searchParameter = searchParameter;
                //get admin role id
                string adminRoleId = (await roleManager.FindByNameAsync("admin")).Id;
                //exclude admin from the result
                users = db.Users.Where(user => user.Roles.FirstOrDefault().RoleId != adminRoleId)
                        .Where(d => d.Email.Contains(searchParameter) || d.FirstName.Contains(searchParameter) || d.LastName.Contains(searchParameter))
                        .ToList();
            }
            else
            {
                //searchParameter is null, i.e. no filter is specified
                //get admin role id
                string adminRoleId = (await roleManager.FindByNameAsync("admin")).Id;
                //exclude admin from the result
                users = db.Users.Where(user => user.Roles.FirstOrDefault().RoleId != adminRoleId).ToList();
            }

            viewModelList = await UserViewModel.CreateFromQueryAsListAsync(users, roleManager);

            //sorting the query
            ViewBag.sortColumn = sortColumn; //this can be null
            ViewBag.sortOrder  = sortOrder;  //this can be null
            switch (sortColumn)
            {
            case "FirstName":
                if (sortOrder == "ASC")
                {
                    viewModelList = viewModelList.OrderBy(d => d.FirstName);
                    ViewBag.newSortOrderForFirstName = "DESC";
                }
                else
                {
                    viewModelList = viewModelList.OrderByDescending(d => d.FirstName);
                    ViewBag.newSortOrderForFirstName = "ASC";
                }
                break;

            case "LastName":
                if (sortOrder == "ASC")
                {
                    viewModelList = viewModelList.OrderBy(d => d.LastName);
                    ViewBag.newSortOrderForLastName = "DESC";
                }
                else
                {
                    viewModelList = viewModelList.OrderByDescending(d => d.LastName);
                    ViewBag.newSortOrderForLastName = "ASC";
                }
                break;

            case "Email":
                if (sortOrder == "ASC")
                {
                    viewModelList = viewModelList.OrderBy(d => d.Email);
                    ViewBag.newSortOrderForEmail = "DESC";
                }
                else
                {
                    viewModelList = viewModelList.OrderByDescending(d => d.Email);
                    ViewBag.newSortOrderForEmail = "ASC";
                }
                break;

            case "Telefon":
                if (sortOrder == "ASC")
                {
                    viewModelList = viewModelList.OrderBy(d => d.Phone);
                    ViewBag.newSortOrderForPhone = "DESC";
                }
                else
                {
                    viewModelList = viewModelList.OrderByDescending(d => d.Phone);
                    ViewBag.newSortOrderForPhone = "ASC";
                }
                break;

            case "Role":
                if (sortOrder == "ASC")
                {
                    viewModelList = viewModelList.OrderBy(d => d.Role);
                    ViewBag.newSortOrderForRole = "DESC";
                }
                else
                {
                    viewModelList = viewModelList.OrderByDescending(d => d.Role);
                    ViewBag.newSortOrderForRole = "ASC";
                }
                break;

            case "DateOfRecord":
                if (sortOrder == "ASC")
                {
                    viewModelList = viewModelList.OrderBy(d => d.DateOfRecord);
                    ViewBag.newSortOrderForRole = "DESC";
                }
                else
                {
                    viewModelList = viewModelList.OrderByDescending(d => d.DateOfRecord);
                    ViewBag.newSortOrderForRole = "ASC";
                }
                break;

            default:
                viewModelList = viewModelList.OrderBy(s => s.UserId);
                break;
            }


            var viewResult = viewModelList.ToPagedList(pageNumber, pageSize);

            return(PartialView("UpdateIndex", viewResult as IEnumerable <UserViewModel>));
        }