public IActionResult ManageProfile() { if (User.IsInRole("Customer")) { string UserName = User.Identity.Name; ApplicationUser user = _userManager.Users.FirstOrDefault(x => x.UserName == UserName); IEnumerable <Address> userAddresses = _addressService.Query(x => x.ApplicationUserId == user.Id).OrderByDescending(y => y.PreferredShippingAddress).ThenBy(z => z.StreetAddress); AccountManageProfileViewModel VM = new AccountManageProfileViewModel() { UserName = user.UserName, GivenNames = user.GivenNames, Surname = user.Surname, Email = user.Email, PhoneNo = user.PhoneNumber, Addresses = userAddresses.ToList() }; return(View(VM)); } else { return(View()); } }
public async Task <IActionResult> ManageProfile(AccountManageProfileViewModel VM) { string UserName = User.Identity.Name; var user = _userManager.Users.FirstOrDefault(x => x.UserName == UserName); user.GivenNames = VM.GivenNames; user.Surname = VM.Surname; user.Email = VM.Email; user.PhoneNumber = VM.PhoneNo; IdentityResult Updateduser = await _userManager.UpdateAsync(user); if (Updateduser.Succeeded) { // Not sure if I'm duplicating code here... _userManager.UpdateAsync(user); return(RedirectToAction("Index", "Home")); } // To do: try catch block around return in case user cant be found or there's an error... return(View(VM)); }