public ActionResult ResetPassword(Business thisBus, int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } //Don't check include in validation check ModelState.Remove("UserEmail"); ModelState.Remove("BusName"); ModelState.Remove("BusDesc"); ModelState.Remove("BusAddress"); if (ModelState.IsValid) { using (Entities dc = new Entities()) { GrizzTime.Models.business bus = dc.businesses.FirstOrDefault(p => p.UserID == id); if (thisBus == null) { return(HttpNotFound()); } bus.UserPW = Hash(thisBus.UserPW); dc.Entry(bus).State = System.Data.Entity.EntityState.Modified; try { dc.SaveChanges(); } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) { Exception exception = dbEx; foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { string message1 = string.Format("{0}:{1}", validationErrors.Entry.Entity.ToString(), validationError.ErrorMessage); //create a new exception inserting the current one //as the InnerException exception = new InvalidOperationException(message1, exception); } } throw exception; } } TempData["message"] = "Success! Please log in."; return(RedirectToAction("Login", "Business")); } else { TempData["message"] = "Couldn't complete request."; } //SendVerificationEMail(thisEmp.UserEmail); return(View(thisBus)); }
public ActionResult Registration([Bind(Exclude = "IsEmailVerified,ActivationCode")] Business thisBus) { //Check if email exists var isExist = IsEmailExist(thisBus.UserEmail); if (isExist) { ModelState.AddModelError("EmailExist", "There is already an account registered with this email address."); return(View(thisBus)); } //ensure that the model exists if (ModelState.IsValid) { //Save to Database try { using (Entities dc = new Entities()) { GrizzTime.Models.business bus = new GrizzTime.Models.business(); bus.UserEmail = thisBus.UserEmail; bus.UserPW = Hash(thisBus.UserPW); bus.BusName = thisBus.BusName; bus.BusDesc = thisBus.BusDesc; bus.BusAddress = thisBus.BusAddress; bus.UserStatus = "Registered"; Response.Cookies.Add(new HttpCookie("UserID", bus.UserID.ToString())); Response.Cookies.Add(new HttpCookie("Role", "Business")); dc.businesses.Add(bus); dc.SaveChanges(); } SendVerificationEMail(thisBus.UserEmail); TempData["message"] = "Registration complete! An email has been sent to you to confirm your registration!"; return(RedirectToAction("Dashboard")); } catch (Exception ex) { ModelState.AddModelError("", ex.Message); return(View(thisBus)); } //using (Entities dc = new Entities()) //{ // dc.businesses.Add(business); // dc.SaveChanges(); // send email to User //} } else { TempData["message"] = "Invalid Request"; return(View(thisBus)); } }
public void SaveNew() { //TODO : validate before save //TODO : automapper using (Entities dc = new Entities()) { GrizzTime.Models.business bus = new GrizzTime.Models.business(); bus.UserEmail = this.UserEmail; bus.UserPW = this.UserPW; bus.BusName = this.BusName; bus.BusDesc = this.BusDesc; bus.BusAddress = this.BusAddress; bus.UserStatus = this.UserStatus; dc.businesses.Add(bus); dc.SaveChanges(); } }
public ActionResult Edit(int?id, Business thisBus) { ViewBag.UserID = Request.Cookies["UserID"].Value; if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } bool Status = false; string message; //Don't check include in validation check //ModelState.Remove("UserEmail"); //ModelState.Remove("EmpFName"); //ModelState.Remove("EmpLName"); //ModelState.Remove("EmpPhone"); //ModelState.Remove("EmpType"); ModelState.Remove("UserPW"); ModelState.Remove("ConfirmPassword"); if (ModelState.IsValid) { using (Entities dc = new Entities()) { GrizzTime.Models.business bus = dc.businesses.FirstOrDefault(p => p.UserID == id); if (thisBus == null) { return(HttpNotFound()); } bus.BusName = thisBus.BusName; bus.BusAddress = thisBus.BusAddress; bus.BusDesc = thisBus.BusDesc; bus.UserEmail = thisBus.UserEmail; bus.UserStatus = thisBus.UserStatus; dc.Entry(bus).State = System.Data.Entity.EntityState.Modified; try { dc.SaveChanges(); message = "Business updated successfully."; Status = true; TempData["message"] = message; ViewBag.Status = Status; return(RedirectToAction("Dashboard")); } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) { //more descriptive error for validation problems Exception exception = dbEx; foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { string message1 = string.Format("{0}:{1}", validationErrors.Entry.Entity.ToString(), validationError.ErrorMessage); //create a new exception inserting the current one //as the InnerException exception = new InvalidOperationException(message1, exception); } } //error for UI ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); throw exception; } } } else { message = "Invalid Request"; } TempData["message"] = message; ViewBag.Status = Status; return(View(thisBus)); }