1

There are two major methods to validate models

  1. Check Model.IsValid in every action
  2. Implement ActionFilterAttribute and which checks !actionContext.ModelState.IsValid every request, register it globally.

Which one do you use? What are pros/cons of them?

2
  • 1
    Pretty broad IMO. Anyway if what you have to do is just is if (!Model.IsValid) then a filter would be right. It's not always true, sometimes you need more (and more specific) logic then it has to be done in each method... Commented Apr 1, 2014 at 9:04
  • I use my custom validation, not Model.IsValid. Every model passes from it's validation lawyer. Commented Apr 1, 2014 at 11:04

1 Answer 1

2

In my experience checking Model.IsValid in every action is a better approach.

  • allows you to use PRG (Post-Redirect-Get) pattern easier
  • sometimes specific actions are needed when user submits invalid form data
  • depending on your ViewModel repopulating dictionaries may be required after invalid form submit
  • allows you to display specific messages for action success/error

I would recommend global ActionFilterAttribute only in most basic solutions.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.