I used to be in the same situation. At that time, I searched, but found that there was no solution that would give you two sets of validation rules in the same class.
The way I decided was to use presentation models. You have the βbasicβ model classes, and you want different user interfaces (in this case, the web interface and the admin user interface) to have different validation rules. In this case, you do not need friend classes for model classes, since you do not want to apply validation rules for the model class itself, instead you need to inherit from your model class to create two presentation model classes, one for websites and one for websites admin interface and apply validation rules using DataAnnotations in different ways on these classes according to your needs. You can also βenhanceβ the view model classes with additional attributes specific to the user interface.
I know that this solution is not ideal, because you will have your own verification rules in two different places, and this is usually not recommended, but it works, and it is not so bad, especially if the application is not very large. The only other solution is to manually check which place the user is using from the user (web or admin), and then add model state errors accordingly. But I would not recommend doing it that way.
I would love to hear if anyone has a better solution for this.
source share