When should I use ModelState.IsValid?

7

When should I use ModelState.isValid ? Is it just in the data insertion? And in the case of update and delete ? It's needed?

    
asked by anonymous 21.06.2016 / 02:25

1 answer

10

Verifying the validity of ModelState must be done every time a form is submitted. Not only for inserts, edits, and deletions.

The ModelState serves not only for Models , but also for ViewModels . The essential function of ModelState is to represent the validity of what was sent to the Controller and in case of non-validity, also detail the errors, if applicable ( ModelState.Errors ).

So, all the fill-in and values-checking work for each property of the submitted object is done by itself. You just have to enter the decoration appropriately in the Model or ViewModel .

    
21.06.2016 / 02:34