Is there a way through data annotations to verify that one date property is greater than or equal to another date property?

醉酒当歌 提交于 2019-12-23 10:10:32

问题


I have a StartDate and EndDate on my SchoolEvents Model and I was wondering if there are any data annotations I could use to verify that the StartDate is less than or equal to the EndDate and that the EndDate is greater than or equal to the StartDate?


回答1:


From my point of view, you have to build a custom validation attribute. You can look at the link to validate follow specific your validation. It will take your efforts so much. Instead of you use data annotation you should apply Fluent Validation which will help you reduce efforts. It is easy to setup, straight forward and separates of concern, you do not need mixing between view models, domain objects, and validations which depend on business rule.




回答2:


You can achieve what you need by installing and using foolproof nuget package.

Install foolproof nuget package and use its extra useful attributes like the following:

public class EventViewModel
{
    [Required]
    public string Name { get; set; }

    [Required]
    public DateTime Start { get; set; }

    [Required]
    [GreaterThan("Start")]
    public DateTime End { get; set; }
}

More examples of exactly what you need are here



来源:https://stackoverflow.com/questions/16400923/is-there-a-way-through-data-annotations-to-verify-that-one-date-property-is-grea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!