【发布时间】:2012-01-19 10:21:25
【问题描述】:
让我们有一个模型
public class Model
{
public int Number { get; set; }
public DateTime Date { get; set; }
}
我观察到以下行为。 Number 属性的值为 0,Date 的值为 1.1 0001 00:00:00,此时未提交任何内容且 ModelState.IsValid 的值为 true。
DataAnnotationsModelValidatorProvider 类和 GetValidators 方法有这段代码
// Add an implied [Required] attribute for any non-nullable value type,
// unless they've configured us not to do that.
if (AddImplicitRequiredAttributeForValueTypes &&
metadata.IsRequired &&
!attributes.Any(a => a is RequiredAttribute)) {
attributes = attributes.Concat(new[] { new RequiredAttribute() });
}
如果我理解这一点,那么 Number 属性和 DateTime 属性应该设置为 RequiredAttribute 并且验证例程应该将模型设置为无效并生成适当的 错误消息。
所以我的问题是为什么模型无效?
我正在使用 ASP.NET MVC 3
【问题讨论】: