0

我看到了 ValidateIsLesser 和 ValidateIsGreater 属性。但是如果我想做一个 ValidateIsLesserOrEqual 和/或 ValidateIsGreaterOrEqual。我是否只需要编写这些自定义属性,或者是否有我很难找到的预建功能?

[ValidateNonEmpty]
[ValidateDate]
[ValidateIsLesser(IsLesserValidationType.Date, "EndDate", "Start Date must be before End Date.")]
public DateTime StartDate { get; set; }

[ValidateNonEmpty]
[ValidateDate]
[ValidateIsGreater(IsGreaterValidationType.Date, "StartDate", "End Date must be after the Start Date.")]
public DateTime EndDate { get; set; }
4

1 回答 1

2

您可以使用ValidateSelf属性并为 OrEqual 比较提供您自己的验证:

[ValidateNonEmpty]
[ValidateDate]
public DateTime StartDate { get; set; }

[ValidateNonEmpty]
[ValidateDate]
public DateTime EndDate { get; set; }

[ValidateSelf]
public void ValidateDate(ErrorSummary errors)
{
    if (StartDate >= EndDate)
        errors.RegisterErrorMessage("StartDate", "Start date must be earlier than end date.");
}
于 2010-01-03T06:49:14.620 回答