Exemplo n.º 1
0
        public async Task<ActionResult> Index(AdminUserViewModel model, string sortOrder, string searchString, string searchRank, ManageMessageId? message = null)
        {
            ViewBag.StatusMessage =
              message == ManageMessageId.UserDeleted ? "User account has successfully been deleted."
              : message == ManageMessageId.UserUpdated ? "User account has been updated."
              : "";

            ViewBag.ErrorMessage =
                message == ManageMessageId.Error ? "An error has occurred."
                : message == ManageMessageId.HighRankedUser ? "This user account cannot be deleted due to its rank."
                : "";

            await ShowUserDetails(model, sortOrder, searchString, searchRank);
            return View();
        }
Exemplo n.º 2
0
        public async Task<ActionResult> ShowUserDetails(AdminUserViewModel model, string sortOrder, string searchString, string searchRank)
        {
            usrList.Clear();
            roleList.Clear();
            ViewBag.RankSortParm = string.IsNullOrEmpty(sortOrder) ? "rank_desc" : "";
            ViewBag.UsernameSortParm = sortOrder == "Username" ? "username_desc" : "Username";

            IList<ApplicationUser> users = _userManager.Users.ToList();

            foreach (var user in users)
            {
                var roles = await _userManager.GetRolesAsync(user);
                model.UserName = user.UserName;
                foreach (var role in roles)
                {
                    model.RankName = role;
                    switch (role)
                    {
                        case "Admin":
                            model.RankId = "1";
                            break;
                        case "Senior":
                            model.RankId = "2";
                            break;
                        case "Moderator":
                            model.RankId = "3";
                            break;
                        case "Member":
                            model.RankId = "4";
                            break;
                        case "Junior":
                            model.RankId = "5";
                            break;
                        case "Candidate":
                            model.RankId = "6";
                            break;
                    }
                }
                model.UserId = user.Id;
                model.UserFullName = user.FirstName + " " + user.LastName;
                usrList.Add(new AdminUserViewModel() { UserName = model.UserName, RankName = model.RankName, UserId = model.UserId, RankId = model.RankId, UserFullName = model.UserFullName });
                model.RankName = null;
            }
            List<AdminRoleViewModel> rlList = new List<AdminRoleViewModel>();
            rlList.Add(new AdminRoleViewModel() { Role = "All", RoleId = "0" });
            rlList.Add(new AdminRoleViewModel() { Role = "Admin", RoleId = "1" });
            rlList.Add(new AdminRoleViewModel() { Role = "Senior", RoleId = "2" });
            rlList.Add(new AdminRoleViewModel() { Role = "Moderator", RoleId = "3" });
            rlList.Add(new AdminRoleViewModel() { Role = "Member", RoleId = "4" });
            rlList.Add(new AdminRoleViewModel() { Role = "Junior", RoleId = "5" });
            rlList.Add(new AdminRoleViewModel() { Role = "Candidate", RoleId = "6" });
            rlList = rlList.OrderBy(x => x.RoleId).ToList();
            foreach (var role in rlList)
            {
                roleList.Add(new SelectListItem { Text = role.Role, Value = role.Role });
            }

            if (searchString != null)
            {
                usrList = usrList.Where(x => x.UserName.Contains(searchString)).ToList();
                AdmNameSrch = searchString;
            }
            if (AdmNameSrch != null)
            {
                usrList = usrList.Where(x => x.UserName.Contains(AdmNameSrch)).ToList();
            }
            if (searchRank != null)
            {
                if (searchRank == "All")
                {
                    usrList = usrList.Where(x => x.RankName == "Admin" || x.RankName == "Senior" || x.RankName == "Moderator" || x.RankName == "Member" || x.RankName == "Junior" || x.RankName == "Candidate").ToList();
                }
                else
                {
                    usrList = usrList.Where(x => x.RankName.Contains(searchRank)).ToList();
                }
                AdmRankSrch = searchRank;
            }
            if (AdmRankSrch != null)
            {
                if (AdmRankSrch == "All")
                {
                    usrList = usrList.Where(x => x.RankName == "Admin" || x.RankName == "Senior" || x.RankName == "Moderator" || x.RankName == "Member" || x.RankName == "Junior" || x.RankName == "Candidate").ToList();
                }
                else
                {
                    usrList = usrList.Where(x => x.RankName.Contains(AdmRankSrch)).ToList();
                }
            }



            switch (sortOrder)
            {
                case "rank_desc":
                    usrList = usrList.OrderByDescending(x => x.RankId).ToList();
                    break;
                case "Username":
                    usrList = usrList.OrderBy(x => x.UserName).ToList();
                    break;
                case "username_desc":
                    usrList = usrList.OrderByDescending(x => x.UserName).ToList();
                    break;
                default:
                    usrList = usrList.OrderBy(x => x.RankId).ToList();
                    break;
            }

            return PartialView("ShowUserDetails");
        }