Custom model validation of dependent properties using Data Annotations

后端 未结 5 1078
迷失自我
迷失自我 2020-11-27 03:30

Since now I\'ve used the excellent FluentValidation library to validate my model classes. In web applications I use it in conjunction with the jquery.validate plugin to per

5条回答
  •  天涯浪人
    2020-11-27 04:09

    It took a little while since your question was asked, but if you still like metadata (at least sometimes), below there is yet another alternative solution, which allows you provide various logical expressions to the attributes:

    [Required]
    public DateTime? StartDate { get; set; }    
    [Required]
    [AssertThat("StartDate != null && EndDate > StartDate")]
    public DateTime? EndDate { get; set; }
    

    It works for server as well as for client side. More details can be found here.

提交回复
热议问题