Warren
Warren

Reputation: 384

HandleErrorAttribute Add Exception Message to ModelState and continue

I would like to catch all the DbEntityValidationException messages, add them to the ModelState and continue to the view that caused the Exception which would display the ModelState errors using @Html.ValidationSummary()

I've got everything working except the Extension class for HandleErrorAttribute I've written only redirects to the /Shared/Error view OR if I write - filterContext.ExceptionHandled = true; it returns a blank view

 public class HandleLogErrorAttribute : HandleErrorAttribute
{
    //Log the filterContext Exception Details
    public override void OnException ( ExceptionContext filterContext )
    {
        var exception = filterContext.Exception;
        var controller = ( (Controller) filterContext.Controller );

        //Log here 

        //Add Validation Exception messages to ModelState here
        if ( exception is DbEntityValidationException ) {
            var dbEx = filterContext.Exception as DbEntityValidationException;
            foreach ( var ves in dbEx.EntityValidationErrors ){
                foreach ( var ve in ves.ValidationErrors ) {
                    controller.ModelState.AddModelError( string.Empty, ve.ErrorMessage );
                }
             }
             //HOW to continue to the View that caused the exception with the model state now filed with the Validation Exceptions?

        }
    }
}

Upvotes: 1

Views: 895

Answers (0)

Related Questions