// GET: User public async Task<ActionResult> Index() { var userList = new List<UserWithRole>(); var users = context.Users.ToList(); var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context)); foreach (var u in users) { var user = new UserWithRole(); user.UserName = u.UserName; user.Email = u.Email; user.PhoneNumber = u.PhoneNumber; user.Id = u.Id; if (u.Roles.Count == 0) user.Role = ""; else { var role = await RoleManager.FindByIdAsync(u.Roles.First().RoleId); user.Role = role.Name; } userList.Add(user); } return View(userList); }
public ActionResult ModifyRole(string id) { var u = context.Users.Where(us => us.Id == id).FirstOrDefault(); var user = new UserWithRole(); user.UserName = u.UserName; user.Email = u.Email; user.PhoneNumber = u.PhoneNumber; user.Id = u.Id; var list = context.Roles.OrderBy(r => r.Name).ToList().Select(rr => new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList(); ViewBag.Roles = list; return View(user); }