public async Task <IActionResult> UpdateProfile(CustomerUpdateProfileViewModel vm, IFormFile file) { if (ModelState.IsValid) { //get the user who already logged in IdentityUser user = await _userManagerService.FindByNameAsync(User.Identity.Name); //get the profile for this user Customer customer = _customerDataService.GetSingle(p => p.UserId == user.Id); //map the vm customer.FirstName = vm.FirstName; customer.LastName = vm.LastName; customer.Phone = vm.Phone; customer.Address = vm.Address; customer.ProfilePhoto = vm.ProfilePhoto; if (file != null) { //&& System.IO.File.Exists(customer.ProfilePhoto) //System.IO.File.Delete(customer.ProfilePhoto); //upload server path var uploadPath = Path.Combine(_environment.WebRootPath, "images"); //create subfolder Directory.CreateDirectory(Path.Combine(uploadPath, User.Identity.Name)); //get the file name string fileName = FileNameHelper.GetNameFormated(Path.GetFileName(file.FileName)); // stream the file to the srever using (var fileStream = new FileStream(Path.Combine(uploadPath, User.Identity.Name, fileName), FileMode.Create)) { await file.CopyToAsync(fileStream); } //add the file url to category customer.ProfilePhoto = User.Identity.Name + "/" + fileName; } //save changes _customerDataService.Update(customer); user.Email = vm.Email; user.UserName = vm.UserName; await _userManagerService.UpdateAsync(user); //go home return(RedirectToAction("Index", "Customer")); } return(View(vm)); }
public ActionResult Detail(CustomerUpdateProfileViewModel model) { if (ModelState.IsValid) { Customer customer = (Customer)Session["Customer"]; customer = db.Customers.Find(customer.Id); customer.Name = model.Name; customer.Phone = model.Phone; customer.Address = model.Address; customer.CreditCard = model.CreditCard; db.Entry(customer).State = EntityState.Modified; db.SaveChanges(); Session["Customer"] = customer; return(Redirect(Request.UrlReferrer.ToString())); } return(View(model)); }
public async Task <IActionResult> UpdateProfile() { //get the user who already logged in IdentityUser user = await _userManagerService.FindByNameAsync(User.Identity.Name); //get the profile for this user Customer customer = _customerDataService.GetSingle(p => p.UserId == user.Id); //create vm CustomerUpdateProfileViewModel vm = new CustomerUpdateProfileViewModel { FirstName = customer.FirstName, LastName = customer.LastName, Phone = customer.Phone, Address = customer.Address, ProfilePhoto = customer.ProfilePhoto, Email = user.Email, UserName = user.UserName }; return(View(vm)); }