public async Task <IActionResult> Create(ModelForCreate model) { User user = new User() { FullName = model.FullName, Email = model.Email, PhoneNum = model.PhoneNum, Password = model.Password, ConfirmPassword = model.ConfirmPassword, /*Address = address,*/ //Avatar_Path = UploadedFile(model) }; _context.User.Add(user); await _context.SaveChangesAsync(); Address address = new Address() { Province = _context.Province.ToList().Find(x => x.Id == model.Province).Name, District = _context.District.ToList().Find(x => x.Id == model.District).Name, Ward = _context.Ward.ToList().Find(x => x.Id == model.Ward).Name, HouseNumber = model.HouseNumber, User = user }; _context.Address.Add(address); await _context.SaveChangesAsync(); return(View()); }
private string UploadedFile(ModelForCreate model) { string uniqueFileName = null; if (model.iformfile_path != null) { string uploadsFolder = Path.Combine(_hostEnvironment.WebRootPath, "images"); uniqueFileName = Guid.NewGuid().ToString() + "_" + model.iformfile_path.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { model.iformfile_path.CopyTo(fileStream); } } return(uniqueFileName); }
public async Task <IActionResult> Create(ModelForCreate model) { if (ModelState.IsValid) { Address address = new Address() { ProvinceId = model.Province, DistrictId = model.District, WardId = model.Ward, HouseNum = model.HouseNumber }; _context.Add(address); await _context.SaveChangesAsync(); ApplicationUser User = new ApplicationUser() { Avatar = AvatarPathForUser(model.iformfile_path), FullName = model.FullName, PhoneNumber = model.PhoneNum, Email = model.Email, UserName = model.Email, AddressId = address.Id }; var result = await _userManager.CreateAsync(User, model.Password); address.ApplicationUserId = User.Id; await _context.SaveChangesAsync(); if (result.Succeeded) { await _signInManager.SignInAsync(User, false); return(RedirectToAction("Index", "Account")); } else { foreach (var item in result.Errors) { ModelState.AddModelError("", item.Description); } } } return(View()); }
public async Task <IActionResult> Create(ModelForCreate model) { if (ModelState.IsValid) { Address address = new Address() { Province = _context.Province.ToList().Find(el => el.Id == model.Province).Name, District = _context.District.ToList().Find(el => el.Id == model.District).Name, Ward = _context.Ward.ToList().Find(el => el.Id == model.Ward).Name, HouseNum = model.HouseNumber }; _context.Add(address); await _context.SaveChangesAsync(); ApplicationUser User = new ApplicationUser() { Avatar_Path = UploadedFile(model), FullName = model.FullName, PhoneNumber = model.PhoneNum, Email = model.Email, UserName = model.Email, Address = address }; var result = await _userManager.CreateAsync(User, model.Password); if (result.Succeeded) { await _signInManager.SignInAsync(User, false); return(RedirectToAction("Index", "Account", User)); } else { foreach (var item in result.Errors) { ModelState.AddModelError("", item.Description); } } } return(View()); }