Data annotations, why does boolean prop.IsRequired always equal true

后端 未结 1 1517
既然无缘
既然无缘 2021-01-15 14:55

I have a model containing a boolean with no [Required] attribute

public bool IsOptedIn { get; set; }

I have overriden Ob

1条回答
  •  囚心锁ツ
    2021-01-15 15:38

    DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false; 
    

    Add this line to your Application_Start method from Global.asax. By default MVC adds [Required] attribute to non-nullable value types (because you can't convert a null into a bool, it must be a bool).

    0 讨论(0)
提交回复
热议问题