I have a DateTime property in my model and I would like to validate it using DataAnnotations.
In my view I want to get that DateTime as a number of days (e.g. “3”), instead of a DateTime (e.g. “7/14/2010”). If user enters “3” in the view, then DataAnnotation finds it invalid -and that is the issue.
What are my options in this scenario?
Thank you very much for your help.
It sounds like you are using the wrong type for the job. A measurement in “days” is a TimeSpan, not a DateTime. I would update your model to expose the property as a TimeSpan. You could validate with a custom validator like the following: