public async Task <ActionResult> Edit(RegisterViewModelEdit register) { if (ModelState.IsValid) { using (ApplicationDbContext context = new ApplicationDbContext()) { try { var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context)); var userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context)); var updateUser = userManager.FindById(register.UserId); updateUser.FirstName = register.FirstName; updateUser.LastName = register.LastName; updateUser.Contact = register.Contact; updateUser.Email = register.Email; updateUser.UserName = register.Email; await userManager.RemoveFromRoleAsync(updateUser.Id, updateUser.Roles.FirstOrDefault()?.RoleId); await userManager.AddToRoleAsync(updateUser.Id, register.Role); await userManager.UpdateAsync(updateUser); return(RedirectToAction("User")); } catch { return(RedirectToAction("BadRequest", "Home")); } } } ViewBag.Roles = new SelectList(await Roles(), "Id", "Name", register.Role); return(View(register)); }
public ActionResult Edit(RegisterViewModelEdit view) { if (ModelState.IsValid) { var user = view.MapToModelEdit(); ApplicationUser userInDB = UserManager.FindById(user.Id); userInDB.Email = user.Email; userInDB.FirstName = user.FirstName; userInDB.LastName = user.LastName; userInDB.UserName = user.Email; userInDB.DateOfBirth = user.DateOfBirth; userInDB.OIB = user.OIB; userInDB.Phone = user.Phone; userInDB.Position = user.Position; userInDB.UserRole = user.UserRole; userInDB.CostCenterId = user.CostCenterId; userInDB.SuperiorId = user.SuperiorId; IdentityResult result = UserManager.Update(userInDB); if (result.Succeeded) { return(RedirectToAction("Index")); } else { AddErrors(result); } } return(RedirectToAction("Index")); }
public async Task <ActionResult> Edit(string id) { var user = new RegisterViewModelEdit(); using (ApplicationDbContext context = new ApplicationDbContext()) { var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context)); var update = context.Users.Find(id); var roleId = update.Roles.FirstOrDefault().RoleId; user.UserId = id; user.FirstName = update.FirstName; user.LastName = update.LastName; user.Contact = update.Contact; user.Email = update.Email; user.Role = roleManager.Roles.Where(r => r.Id == roleId).FirstOrDefault().Name; } ViewBag.Roles = new SelectList(await Roles(), "Id", "Name", user.Role); return(View(user)); }
// mapiranje iz RegisterViewModelEdit u Application user-a public static ApplicationUser MapToModelEdit(this RegisterViewModelEdit view) { if (view == null) { return(null); } return(new ApplicationUser() { Id = view.Id, FirstName = view.FirstName, LastName = view.LastName, UserName = view.Email, Email = view.Email, DateOfBirth = view.DateOfBirth, OIB = view.OIB, Phone = view.Phone, Position = view.Position, UserRole = view.UserRole, CostCenterId = view.CostCenterId, SuperiorId = view.SuperiorId }); }