public static void ApplyStateChanges(this IDbContext context) { foreach (var entry in context.ChangeTracker.Entries <IObjectWithState>()) { var stateInfo = entry.Entity; entry.State = ModelStateHelpers.ConvertState(stateInfo.State); stateInfo.State = ModelState.Unchanged; } }
public override void OnActionExecuted(ActionExecutedContext context) { // Only export if ModelState is not valid. if (context.ModelState != null && !context.ModelState.IsValid && IsRedirect(context)) { var controller = context.Controller as Controller; if (controller != null) { controller.TempData[Key] = ModelStateHelpers.SerializeModelState(context.ModelState); } } base.OnActionExecuted(context); }
public override void OnActionExecuted(ActionExecutedContext filterContext) { if (filterContext.Result is ViewResult && filterContext.ActionDescriptor is ControllerActionDescriptor cad) { var json = filterContext.HttpContext.Session.GetString(cad.ActionName); if (!string.IsNullOrEmpty(json)) { var modelState = ModelStateHelpers.DeserializeModelState(json); filterContext.ModelState.Merge(modelState); filterContext.HttpContext.Session.Remove(cad.ActionName); } } base.OnActionExecuted(filterContext); }
public override void OnActionExecuted(ActionExecutedContext filterContext) { if (!filterContext.ModelState.IsValid && filterContext.ActionDescriptor is ControllerActionDescriptor cad) { if (filterContext.Result is RedirectResult || filterContext.Result is RedirectToRouteResult || filterContext.Result is RedirectToActionResult) { var modelState = ModelStateHelpers.SerializeModelState(filterContext.ModelState); filterContext.HttpContext.Session.SetString(cad.ActionName, modelState); } } base.OnActionExecuted(filterContext); }
// POST api/TodoList public HttpResponseMessage PostTodoItem(TodoItem todoitem) { if (ModelState.IsValid) { var user = _db.Users.Single(x => x.Username == HttpContext.Current.User.Identity.Name); todoitem.UserId = user.Id; _db.TodoItems.Add(todoitem); _db.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.Created, todoitem)); } return(Request.CreateResponse(HttpStatusCode.BadRequest, new { errors = ModelStateHelpers.GetErrorsFromModelState(ModelState) })); }
public HttpResponseMessage Login(LoginModel model) { if (ModelState.IsValid) { if (WebSecurity.Login(model.Username, model.Password, model.RememberMe)) { FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe); return(Request.CreateResponse(HttpStatusCode.OK)); } ModelState.AddModelError("", "The user name or password provided is incorrect."); } // If we got this far, something failed return(Request.CreateResponse(HttpStatusCode.BadRequest, new { errors = ModelStateHelpers.GetErrorsFromModelState(ModelState) })); }
public override void OnActionExecuted(ActionExecutedContext filterContext) { //Only export when ModelState is not valid if (!filterContext.ModelState.IsValid) { //Export if we are redirecting if (filterContext.Result is RedirectResult || filterContext.Result is RedirectToRouteResult || filterContext.Result is RedirectToActionResult) { var controller = filterContext.Controller as Controller; if (controller != null && filterContext.ModelState != null) { var modelState = ModelStateHelpers.SerialiseModelState(filterContext.ModelState); controller.TempData[Key] = modelState; } } } base.OnActionExecuted(filterContext); }
// PUT api/TodoList/ public HttpResponseMessage PutTodoItem(TodoItem todoitem) { if (!ModelState.IsValid) { return(Request.CreateResponse(HttpStatusCode.BadRequest, new { errors = ModelStateHelpers.GetErrorsFromModelState(ModelState) })); } _db.Entry(todoitem).State = EntityState.Modified; try { _db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex)); } return(Request.CreateResponse(HttpStatusCode.OK)); }
public void OnPageHandlerExecuted(PageHandlerExecutedContext context) { var pageModel = context.HandlerInstance as PageModel; var serializedModelState = pageModel?.TempData[ModelStateTransferAttribute.Key] as string; if (serializedModelState != null) { // Only Import if we are viewing. if (context.Result is PageResult) { var modelState = ModelStateHelpers.DeserializeModelState(serializedModelState); context.ModelState.Merge(modelState); } else { // Otherwise remove it. pageModel.TempData.Remove(ModelStateTransferAttribute.Key); } } }
public override void OnActionExecuted(ActionExecutedContext filterContext) { var controller = filterContext.Controller as Controller; var serialisedModelState = controller?.TempData[Key] as string; if (serialisedModelState != null) { //Only Import if we are viewing if (filterContext.Result is ViewResult) { var modelState = ModelStateHelpers.DeserialiseModelState(serialisedModelState); filterContext.ModelState.Merge(modelState); } else { //Otherwise remove it. controller.TempData.Remove(Key); } } base.OnActionExecuted(filterContext); }
//[ValidateAntiForgeryToken] public HttpResponseMessage Register(RegisterModel model) { if (ModelState.IsValid) { // Attempt to register the user try { WebSecurity.CreateUserAndAccount(model.Username, model.Password); WebSecurity.Login(model.Username, model.Password); FormsAuthentication.SetAuthCookie(model.Username, createPersistentCookie: false); return(Request.CreateResponse(HttpStatusCode.OK)); } catch (MembershipCreateUserException e) { ModelState.AddModelError("", ErrorCodeToString(e.StatusCode)); } } // If we got this far, something failed return(Request.CreateResponse(HttpStatusCode.BadRequest, new { errors = ModelStateHelpers.GetErrorsFromModelState(ModelState) })); }