Created
March 24, 2016 14:31
-
-
Save mii9000/0b447d7e56b51ede76b3 to your computer and use it in GitHub Desktop.
Instead of checking for ModelState is valid or not in every action, encapsulate this check in a Action Filter.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ValidateModelAttribute: ActionFilterAttribute | |
{ | |
public override void OnActionExecuting(ActionExecutingContext filterContext) | |
{ | |
if (filterContext.Controller.ViewData.ModelState.IsValid) | |
{ | |
return; | |
} | |
filterContext.Result = new ViewResult | |
{ | |
ViewData = filterContext.Controller.ViewData | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment