When should I use ModelState.isValid
? Is it just in the data insertion? And in the case of update
and delete
? It's needed?
When should I use ModelState.isValid
? Is it just in the data insertion? And in the case of update
and delete
? It's needed?
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 .