//From https://andrewlock.net/post-redirect-get-using-tempdata-in-asp-net-core/ 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); }
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); }