public ActionResult Edit(RegistrationDTO registrationDTO) { try { if (!ValidateUser(registrationDTO.Id)) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } using (var db = new Models.HacForoContainer()) { if (registrationDTO.IsValid(db, ModelState)) { var userDb = db.UserSet.Find(registrationDTO.Id); if (userDb == null) { return(HttpNotFound()); } registrationDTO.UpdateModel(userDb); db.SaveChanges(); return(View("Details", Mapper.MapTo(userDb))); } else { ModelState.AddModelError("", "The data is not valid."); } } } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } return(View()); }
public ActionResult ChangePassword(ChangePasswordDTO newChangePassword) { try { if (ModelState.IsValid) { using (var db = new Models.HacForoContainer()) { HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; UserDTO cookieUser = SessionManager.GetLoggedUser(authCookie); var userDb = db.UserSet.Find(cookieUser.Id); if (userDb == null) { return(HttpNotFound()); } userDb.SetPassword(newChangePassword.Password); db.SaveChanges(); return(View("Details", Mapper.MapTo(userDb))); } } else { ModelState.AddModelError("", "The data is not correct"); } } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } return(View()); }
public ActionResult Register(RegistrationDTO registrationDTO) { try { if (ModelState.IsValid) { using (var db = new Models.HacForoContainer()) { if (registrationDTO.IsValid(db, ModelState)) { db.UserSet.Add(RegistrationMap.MapTo(registrationDTO)); db.SaveChanges(); return(RedirectToAction("Index", "Home")); } } } else { ModelState.AddModelError("", "The data is not correct"); } } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } return(View()); }