[CustomValidation(typeof(ValidationMethods), "ValidateGreaterOrEqualToZero")]
public int IntValue { get; set; }
[CustomValidation(typeof(ValidationMethods), "ValidateGreaterOrEqualToZero")]
public decimal DecValue { get; set; }
验证方法类型:
public class ValidationMethods
{
public static ValidationResult ValidateGreaterOrEqualToZero(decimal value, ValidationContext context)
{
bool isValid = true;
if (value < decimal.Zero)
{
isValid = false;
}
if (isValid)
{
return ValidationResult.Success;
}
else
{
return new ValidationResult(
string.Format("The field {0} must be greater than or equal to 0.", context.MemberName),
new List<string>() { context.MemberName });
}
}
}
[Range(typeof(decimal), ((double)0).ToString(), ((double)decimal.MaxValue).ToString(), ErrorMessage = "Amount must be greater than or equal to zero.")]
[Range(typeof(Decimal), "0", "9999", ErrorMessage = "{0} must be a decimal/number between {1} and {2}.")]
[Range(typeof(Decimal),"0.0", "1000000000000000000"]