Adding and removing data annotations from code

Is it possible to add and remove DataAnnotations, in particular [requried], from the code side? My problem is that I want to give the user the ability to save an incomplete form in our CRUD applications, but at the same time use the data validation authority of DataAnnotations.

If this is not possible, what is the best way to do this?

+3
source share
2 answers

It is not possible to add, delete, or modify DataAnnotations dynamically because they are attributes. Attributes are part of the type and cannot be changed at run time.

You can use ModelState, as Larsenal suggested, if:

  • . ( ModelState . , , )
  • , DataAnnotationValidators, , .
0

DataAnnotation , . :

if (certainCondition == true) {
   ModelState["someKey"].Errors.Clear();
   ModelState["anotherKey"].Errors.Clear();
}
+2

Source: https://habr.com/ru/post/1760394/


All Articles