【问题标题】:Is there a way through data annotations to verify that one date property is greater than or equal to another date property?有没有办法通过数据注释来验证一个日期属性是否大于或等于另一个日期属性?
【发布时间】:2013-05-06 14:30:04
【问题描述】:

我的SchoolEvents 模型上有一个StartDateEndDate,我想知道是否有任何数据注释可以用来验证StartDate 是否小于或等于EndDateEndDate 是否大于或等于 StartDate

【问题讨论】:

    标签: c# asp.net asp.net-mvc-4 data-annotations


    【解决方案1】:

    在我看来,你必须建立一个custom validation attribute。您可以查看link 以验证遵循特定的验证。这需要你付出很多努力。 您应该使用Fluent Validation 而不是使用数据注释,这将帮助您减少工作量。它易于设置、直接和关注点分离,您不需要在依赖于业务规则的视图模型、域对象和验证之间混合。

    【讨论】:

      【解决方案2】:

      你可以通过安装和使用foolproofnuget包来实现你所需要的。

      安装foolproof nuget 包并使用其额外有用的属性,如下所示:

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

      更多你需要的例子是here

      【讨论】:

      • 万全不支持MVC3+,最后一个版本是2012年
      猜你喜欢
      • 2014-10-12
      • 1970-01-01
      • 1970-01-01
      • 2015-11-17
      • 1970-01-01
      • 2012-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多