I’ve got my model which contains some members:
public class Address
{
public Street { get; set;}
public City { get; set; }
public PostalCode { get; set; }
}
Now I’ve got my ValidationAttribute with IsValid method overrided like this:
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var input = value as Address;
if (string.IsNullOrEmpty(input.City))
return new ValidationResult("City is required);
if (!string.IsNullOrEmpty(input.PostalCode))
if (string.IsNullOrEmpty(input.Street))
return new ValidationResult("Stret is required");
return ValidationResult.Success;
}
The problem is:
After validation my model state adds model error only to whole Adress member, but I need it to be added to specified members like city or street.
Any help with this will be appreciated… thanks!
you can add memberNames !
EDIT :
i’ve tested and found a solution : System.ComponentModel.IDataErrorInfo !!
this doesn’t work on the javascript client, but when submitting the result is what we attempted 🙂
so keep using the ValidationResult with memberNames
and :
and now the current property is marked in error, and the memberNames too !
😀
Edit 2
In fact, you can keep simple DataAnnotations to inform client,
and server side, you can add more complex business validation :
note that the Validate method is called only when DataAnnotations are all valid !