public ActionResult Edit(int id, UserModel Model) { if (Model == null) { return this.RedirectToAction("Edit", new {id}); } if (this.ModelState.IsValid && id < 1) { if (string.IsNullOrEmpty(Model.Email)) { this.ModelState.AddModelError("Email", "Email is required to create a new user"); } if (string.IsNullOrEmpty(Model.Password)) { this.ModelState.AddModelError("Password", "Password is required for new users"); } } if (!this.userRepository.EmailAvailable(id, Model.Email)) { this.ModelState.AddModelError("Email", "Email is in use, please choose a new username"); } // TODO: Validate that the password is complex enough if (this.ModelState.IsValid) { User user = this.userRepository.GetById(id); if (user == null) { if (id > 0) { return this.View("NotFound"); } user = new User { Email = Model.Email, Salt = "notnull", Password = "******" }; } this.UserFromModel(user, Model); this.userRepository.Save(user); this.loginService.SaveEmailPasswordChanges(user, Model.Email, Model.Password); return this.RedirectToAction("Index"); // Success } return this.View(Model); // Fix your errors }
private UserModel UserToModel(User User) { UserModel model = new UserModel { UserId = User.UserId, Email = User.Email, FirstName = User.FirstName, LastName = User.LastName, IsAdmin = User.IsAdmin, IsElectable = User.IsElectable, IsActive = User.IsActive, }; return model; }
private void UserFromModel(User User, UserModel Model) { User.FirstName = Model.FirstName; User.LastName = Model.LastName; User.IsAdmin = Model.IsAdmin; User.IsElectable = Model.IsElectable; User.IsActive = Model.IsActive; // Specifically don't set Email and Password -- those are set differently }