How do I validate a property from a different class with DataAnnotations
I have a competition class and a competitionEntry class. A competition can have 5 questions (just string values) and the competitionEnt开发者_如何学JAVAry can have 5 answers (also only string values).
How would I validate that if a question is present that the answer should be present as well?
I think I need a custom validation attribute but how would I get the competition class properties to check if the answer is filled in and so the questions must also be?
I don't think you can do this with an attribute. It makes more sense to have the Competition class validate entries.
public class Competition
{
public bool AnswersAreCorrectFor(CompetitionEntry entry)
{
// check answers
}
// ...
}
I haven't done this but AFAIK, you can definitely do it with DataAnnotations.
You can implement a subclass of ValidationAttribute.
Check out this question to get the idea.
精彩评论