我無法正常工作。我想允許用戶選擇start date
和end date
,如果start date
大於end date
我想要顯示一條錯誤消息。我正在使用MVC
。這是我在模型上的代碼。MVC開始日期和結束日期驗證
public class ModelClass : IValidatableObject
{
[Required(ErrorMessage = "ID Number is required")]
[Display(Name = "ID Number:")]
[RegularExpression(@"^(\d{13})$", ErrorMessage = "Enter a 13 digit ID number")]
public Int64 ID_Number { get; set; }
[Required(ErrorMessage = "The start date is required")]
[Display(Name = "Start Date:")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MMM/yyyy}")]
public DateTime Start_Date { get; set; }
[Required(ErrorMessage = "The end date is required")]
[Display(Name = "End Date:")]
[GreaterThan("Start_Date")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MMM/yyyy}")]
public DateTime End_Date { get; set; }
IEnumerable<ValidationResult> IValidatableObject.Validate(ValidationContext validationContext)
{
if (End_Date < Start_Date)
{
yield return new ValidationResult("EndDate must be greater than StartDate");
}
}
}
你可能需要像[這]自定義驗證(http://stackoverflow.com/questions/10887824/greater-than-or-equal -to-today-date-validation-annotation-in-mvc3)。 –
那你有什麼問題? –