I have an simple class that I get from a webservice.
public class person
{
public string name { get; set; }
public int number { get; set; }
}
And a second class in my code
public class people
{
[ValidatePerson]
public List<person> myPeople;
}
and a custom ValidationAttribute
[AttributeUsage(AttributeTargets.Property,AllowMultiple = false)]
public class ValidatePerson : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
ValidationResult returnValue = ValidationResult.Success;
return returnValue; // do stuff later
}
}
Because person comes from a webservice I can’t add a decorator to it (that I know of) and adding the decorator to the attribute in people means that “value” is a List<person> not a person.
The reason I want to validated person not List<person> is because I’m using @Html.ValidationMessageFor(model => model); in the editortemplate for person and I’d like to have multiple messages like “this is not a valid person” not a block at the top that says “you have 3 invalid people”.
Simply create a new model for person you want to validate, i.e:
Then copy the result of the webservice to your personValidator class, and use it in the View.