这是我的整个计划。我希望它返回false
,但结果是true
。我是否期望错误的结果或我在这里做错了什么?
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel.DataAnnotations;
public class Program
{
public static void Main()
{
var c = new Ax(){Id = 1000, Name = "A"};
//c.Name = null; // when un-comment this, result as expected
var context = new ValidationContext(c);
var isValid = Validator.TryValidateObject(c, context, null);
Console.WriteLine(isValid);
}
public class Ax
{
[Range(1,100)] // I expect this to cause failed validation
public int Id{get; set;}
[Required]
public string Name { get; set; }
}
}
结果:是真的