DataAnnotation for Required property

后端 未结 4 851
生来不讨喜
生来不讨喜 2020-12-05 00:24

First it works, but today it failed!

This is how I define the date property:

[Display(Name = \"Date\")]
[Required(ErrorMessage = \"Date of Submissio         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-05 00:48

    UPDATE 24-5-2013: The InvalidModelValidatorProvider responsible for this error message has been removed from the ASP.NET technology stack. This validator proofed to cause more confusion than it was meant to solve. For more information, see the following link: http://aspnetwebstack.codeplex.com/workitem/270

    When you decorate your class with [DataContract] attribute, you need to explicitly decorate the members you would want to serialize with the [DataMember] attribute.

    The issue is that DataContractSerializer does not support the [Required] attribute. For reference types, we're able to check that the value is not null after deserialization. But for value types, there is no way for us to enforce the [Required] semantics for DataContractSerializer without [DataMember(IsRequired=true)].

    So you could end up marking an DateTime as [Required] and expect a model validation error if the DateTime isn't sent, but you'd just get a DateTime.MinValue value and no validation error instead.

提交回复
热议问题