public async Task<ActionResult> Index(AdminUserViewModel model, string sortOrder, string searchString, string searchRank, int? page, 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, page); return View(); }
public async Task<ActionResult> ShowUserDetails(AdminUserViewModel model, string sortOrder, string searchString, string searchRank, int? page) { usrList.Clear(); roleList.Clear(); ViewBag.CurrentSort = sortOrder; ViewBag.RankSortParm = string.IsNullOrEmpty(sortOrder) ? "rank_desc" : ""; ViewBag.UsernameSortParm = sortOrder == "Username" ? "username_desc" : "Username"; IList<ApplicationUser> users = context.Users.ToList(); foreach (var user in users) { var roles = await UserManager.GetRolesAsync(user.Id); 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", RoleValue = "" }); rlList.Add(new AdminRoleViewModel() { Role = "Admin", RoleId = "1", RoleValue = "Admin" }); rlList.Add(new AdminRoleViewModel() { Role = "Senior", RoleId = "2", RoleValue = "Senior" }); rlList.Add(new AdminRoleViewModel() { Role = "Moderator", RoleId = "3", RoleValue = "Moderator" }); rlList.Add(new AdminRoleViewModel() { Role = "Member", RoleId = "4", RoleValue = "Member" }); rlList.Add(new AdminRoleViewModel() { Role = "Junior", RoleId = "5", RoleValue = "Junior" }); rlList.Add(new AdminRoleViewModel() { Role = "Candidate", RoleId = "6", RoleValue = "Candidate" }); rlList = rlList.OrderBy(x => x.RoleId).ToList(); foreach (var role in rlList) { roleList.Add(new SelectListItem { Text = role.Role, Value = role.RoleValue }); } 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) { usrList = usrList.Where(x => x.RankName.Contains(searchRank)).ToList(); AdmRankSrch = searchRank; } if (AdmRankSrch != null) { 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; } int pageSize = 4; int pageNumber = (page ?? 1); return PartialView("ShowUserDetails", usrList.ToPagedList(pageNumber, pageSize)); }