public async Task <ActionResult> Create(UserView userView) { User user = null; try { if (ModelState.IsValid && userView.Photo != null) { string fileName = SetPhotoPath(userView); string path = "/img/" + Path.GetFileName(userView.Photo.FileName); user = new User() { Name = userView.Name, Birthdate = userView.Birthdate, Age = userView.Age, PhotoPath = path }; Role userRole = await Repository.GetRoles().FirstOrDefaultAsync(r => r.Name == "user"); if (userRole != null) { user.Role = userRole; } Repository.AddUser(user); Repository.Save(); return(RedirectToAction("Index")); } } catch (DataException) { ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); } return(View(user)); }
public async Task <ActionResult> Register(RegisterModel model) { try { if (ModelState.IsValid) { User user = await repository.GetUsers().FirstOrDefaultAsync(u => u.Email == model.Email); if (user == null) { user = new User { Name = model.Name, Birthdate = model.Birthdate, Email = model.Email, Password = model.Password }; Role userRole = await repository.GetRoles().FirstOrDefaultAsync(r => r.Name == "user"); if (userRole != null) { user.Role = userRole; } repository.AddUser(user); repository.SaveAsync(); await Authenticate(user); return(RedirectToAction("Index", "Home")); } else { ModelState.AddModelError("", "Incorrect login or password!"); } } } catch (DataException) { ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); } return(View(model)); }