I have some DateTime fields on my MVC model classes – some which require a Date as input and others that require a Time as input – but both are DateTime properties.
Is it possible to have an EditorTemplate for DateTime that somehow produces a date picker to properties that are meant to be dates, and a time picker for properties that are meant to be times?
Yes, here is one way:
In
~/Views/Shared/EditorTemplates(or~/Views/Shared/DisplayTemplates, create template files that use your favourite view engine (example uses Razor/C#)file Date.cshtml
file Time.cshtml
Then, in your model:
The
UIHintattribute name has to match the file name of your template, andUIHintis inSystem.ComponentModel.DataAnnotations, so you will need the appropriate using statement/assembly reference if you don’t have it already.Alternatively, use a
TimeSpanto represent your times – that is whatDateTimereturns for itsTimeOfDayproperty…