Get ViewModel in OnException MVC

How to get data of ViewModel properties in the OnException method, which is published from the page. In my case, if any error occurs on page i, it is redirected to the error page using OnException, but when I click the back button to go to the page, all the field data is cleared, if I get the viewmodel properties in the onexception method, I I can redirect to a page with these properties.

 protected override void OnException(ExceptionContext filterContext)
        {
            Exception exception = filterContext.Exception;
            //Write Exception to Database
            AppLogging.WriteError(exception);

            string controllerName = (string)filterContext.RouteData.Values["controller"];
            string actionName = (string)filterContext.RouteData.Values["action"];
            // Preserve old ViewData here
            var viewData = new ViewDataDictionary<HandleErrorInfo>(filterContext.Controller.ViewData);

            // Set the Exception information model here
            viewData.Model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
            TempData["ErrModel"] = viewData.Model;           

            filterContext.ExceptionHandled = true;
            filterContext.HttpContext.Response.Clear();
            filterContext.HttpContext.Response.StatusCode = 500;
            filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
            filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary { { "area", "" }, { "action", "Error" }, { "controller", "Error" } });

            // Avoid IIS7 getting in the middle
            Response.TrySkipIisCustomErrors = true;
        }
+4
source share

All Articles