爲什麼總是返回true?DataAnnotations在使用驗證器的值類型中似乎無法正確運行
class Program
{
static void Main(string[] args)
{
Person p = new Person();
p.Age = 24;
ICollection<ValidationResult> results = new Collection<ValidationResult>();
bool isValid = Validator.TryValidateObject(p, new ValidationContext(p, null, null), results);
Console.WriteLine("Valid = {0}",isValid);
foreach (var result in results)
{
Console.WriteLine(result.ErrorMessage);
}
Console.ReadKey();
}
}
public class Person
{
[Required(ErrorMessage = "You have to identify yourself!!")]
public int Id { get; set; }
public decimal Age { get; set; }
}
我的用法有什麼問題?
@Erik Phillips:感謝編輯標題......現在更有意義 – Perpetualcoder 2012-04-12 15:50:03