public ActionResult GetUser(int?id = null) { var model = new AdminNewUserModel(); if (id != null) { var user = ParticipantContext.Users.Single(id); model = Mapper.Map <User, AdminNewUserModel>(user); } model.HttpReferer = Request.UrlReferrer.PathAndQuery; return(View("User", model)); }
public ActionResult PostUser(AdminNewUserModel model) { // ** Prototype pattern. the user object which has its default values set var user = Mapper.Map <AdminNewUserModel, User>(model, new User(true)); if (user.Id != null) // existing user { // remove email and password validation. not required for update ModelState.Remove("Email"); ModelState.Remove("Password"); if (ModelState.IsValid) { if (!user.IsValid) // shows how custom validation would be used { // examine user.Errors list } ParticipantContext.Users.Update(user); Success = "User " + user.FullName + " was successfully updated."; return(RedirectToAction("Users")); } } else // new user { if (ModelState.IsValid) { try { // ** Facade pattern. Unit of Work pattern. Service.InsertUser(user, model.Password, "Member"); Success = "User " + user.FullName + " was successfully added."; return(RedirectToAction("Users")); } catch (MembershipCreateUserException e) { Failure = ErrorCodes.Find(e.StatusCode); } } } return(View(model)); }