public class ExcludeChar : ValidationAttribute
{
string _characters;
public ExcludeChar(string characters)
{
_characters = characters;
}
protected override ValidationResult IsValid(object value, System.ComponentModel.DataAnnotations.ValidationContext validationContext)
{
if (value != null)
{
for (int i = 0; i < _characters.Length; i++)
{
var valueAsString = value.ToString();
if (valueAsString.Contains(_characters[i]))
{
var errorMessage = FormatErrorMessage(validationContext.DisplayName);
return new ValidationResult(errorMessage);
}
}
}
return ValidationResult.Success;
}
}
[ExcludeChar("@#$",ErrorMessage = "Please select a option.")]
public string LSESelected1
{
}
我在没有validationContext的情况下调用ViewModel类中的一个验证,验证工作正常,如果我检查validationContext它总是显示“我为空”。