// Enable initialization with an instance of ApplicationUser: public SelectUserRolesViewModel(ApplicationUser user) : this() { this.UserName = user.UserName; this.FirstName = user.FirstName; this.LastName = user.LastName; var Db = new ApplicationDbContext(); var um = new UserManager <ApplicationUser>( new UserStore <ApplicationUser>(new ApplicationDbContext())); // Add all available roles to the list of EditorViewModels: var allRoles = Db.Roles; foreach (var role in allRoles) { // An EditorViewModel will be used by Editor Template: var rvm = new SelectRoleEditorViewModel(role); this.Roles.Add(rvm); } // Set the Selected property to true for those roles for // which the current user is a member: var currentRoles = new List <string>(); currentRoles.AddRange(um.GetRoles(user.Id)); foreach (var userRole in currentRoles) { var checkUserRole = this.Roles.Find(r => r.RoleName == userRole); checkUserRole.Selected = true; } }
// Enable initialization with an instance of ApplicationUser: public SelectGroupRolesViewModel(Group group, string UserName) : this() { this.GroupId = group.Id; this.GroupName = group.Name; this.Ma_TTP = group.Ma_TTP; var Db = new ApplicationDbContext(); // Add all available roles to the list of EditorViewModels: var allRoles = new List <ApplicationRole>(); if (UserName.ToUpper() == "ADMIN") { allRoles = Db.Roles.ToList(); } else { allRoles = Db.Roles.Where(r => r.Ma_TTP == this.Ma_TTP).ToList(); } foreach (var role in allRoles) { // An EditorViewModel will be used by Editor Template: var rvm = new SelectRoleEditorViewModel(role); this.Roles.Add(rvm); } // Set the Selected property to true for those roles for // which the current user is a member: foreach (var groupRole in group.Roles) { var checkGroupRole = this.Roles.Find(r => r.RoleName == groupRole.Role.Name); checkGroupRole.Selected = true; } }