// To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync(int?id, string[] selectedRoles) { if (id == null) { return(NotFound()); } var userToUpdate = await _context.Users .Include(i => i.UsersRoles) .ThenInclude(i => i.Role) .FirstOrDefaultAsync(s => s.UserId == id); if (userToUpdate == null) { return(NotFound()); } if (await TryUpdateModelAsync <User>( userToUpdate, "User", i => i.LoginName, i => i.FullName, i => i.Email, i => i.Password)) { UpdateUserRoles(_context, selectedRoles, userToUpdate); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } UpdateUserRoles(_context, selectedRoles, userToUpdate); PopulateAssignedRoleData(_context, userToUpdate); return(Page()); }
public async Task <IActionResult> OnPostAsync(string[] selectedRoles) { var newUser = new User(); if (selectedRoles != null) { newUser.UsersRoles = new List <UsersRoles>(); foreach (var course in selectedRoles) { var courseToAdd = new UsersRoles { RoleId = int.Parse(course) }; newUser.UsersRoles.Add(courseToAdd); } } if (await TryUpdateModelAsync <User>( newUser, "User", i => i.LoginName, i => i.FullName, i => i.Email, i => i.Password)) { _context.Users.Add(newUser); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } PopulateAssignedRoleData(_context, newUser); return(Page()); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } User user = await _context.Users .Include(i => i.UsersRoles) .SingleAsync(i => i.UserId == id); if (user == null) { return(RedirectToPage("./Index")); } _context.Users.Remove(user); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }