public ActionResult AddUser(UserViewModel model) { ValidPasswordPolicy(model.NewPassword, model.FullPropertyName(m => m.NewPassword)); if (ModelState.IsValid) { var loginUp = model.GetLoginUpper(); var extant = (from u in PrdnDBContext.Users where u.Login == loginUp select new { u.Login }).FirstOrDefault(); if (extant != null) { ModelState.AddModelError(model.FullPropertyName(m => m.Login), "Already assigned to another user."); } } if (ModelState.IsValid) { User user = Mapper.Map<UserViewModel, User>(model); user.PlainPassword = model.NewPassword; PrdnDBContext.Users.AddObject(user); PrdnDBContext.SaveChanges(); return RedirectToAction(actionName: "Users"); } return View(model); }
public ActionResult EditUser(UserViewModel model) { if (!String.IsNullOrEmpty(model.NewPassword)) { ValidPasswordPolicy(model.NewPassword, model.FullPropertyName(m => m.NewPassword)); } if (ModelState.IsValid) { User user = (from x in PrdnDBContext.Users where x.ID == model.ID select x).FirstOrDefault(); if (user == null) { return ErrMsgView("Sorry - Invalid user ID " + model.ID.ToString()); } Mapper.Map(model, user); if (!String.IsNullOrWhiteSpace(model.NewPassword)) { user.PlainPassword = model.NewPassword; } PrdnDBContext.SaveChanges(); return RedirectToAction(actionName: "Users"); } return View(model); }