Can I check ModelState.IsValid
in my custom action filter in OnActionExecuting
method?
Below the Form, the ModelState. IsValid property is checked and if the Model is valid, then the value if the ViewBag object is displayed using Razor syntax in ASP.Net MVC.
The ModelState can be accessed in View using the ViewData object in ASP.Net MVC Razor.
Errors property and the ModelState. IsValid property. They're used for the second function of ModelState : to store the errors found in the submitted values.
Yes. ModelState is part ViewData. So you can get it using:
filterContext.Controller.ViewData.ModelState
For example, if you wanted to inject some code after the action executes, but only if ModelState.IsValid == true
, you can do:
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (!filterContext.Controller.ViewData.ModelState.IsValid) return;
// do something
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With