Diciamo che ho un DataInizio e una data di fine e io Wnt per verificare se il DataFine non è più di 3 mesi di distanza dalla data di inizioConfronta date DataAnnotations convalida asp.net mvc
public class DateCompare : ValidationAttribute
{
public String StartDate { get; set; }
public String EndDate { get; set; }
//Constructor to take in the property names that are supposed to be checked
public DateCompare(String startDate, String endDate)
{
StartDate = startDate;
EndDate = endDate;
}
public override bool IsValid(object value)
{
var str = value.ToString();
if (string.IsNullOrEmpty(str))
return true;
DateTime theEndDate = DateTime.ParseExact(EndDate, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
DateTime theStartDate = DateTime.ParseExact(StartDate, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture).AddMonths(3);
return (DateTime.Compare(theStartDate, theEndDate) > 0);
}
}
e vorrei per implementare questo nel mio convalida
[DateCompare ("StartDate", "EndDate", ErrorMessage = "The Deal non può che essere lunga 3 mesi!")]
so ottengo un errore qui ... ma come posso fare questo tipo di convalida delle regole di business in asp.net mvc
c'è una risposta per questo? Oliver, cosa funziona per te? –