I am using ASP.Net4 and MVC2. I am getting a "Nullable object must have a value" error. In one of the model class I am validating the submitted form values against business rules. One of which checks if Date1 and Date2 are within 30 days of each other. The Date1 and Date2 are Nullable DateTime(DateTime?). The rule is as follows
if ((Date1.HasValue && Date2.HasValue) &&
Date1.Value.Subtract((DateTime)Date2).TotalDays > 30 ||
Date1.Value.Subtract((DateTime)Date2).TotalDays < -30)
yield return new RuleViolation ("Date1 and Date2 must be within 30 days of each other","Date1");
Here I check both dates to make sure there are not null before doing the substraction. What am I doing wrong her? Thanks.
View Complete Post