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
replace this with a real date picker
file Time.cshtml
replace this with a real time picker
Then, in your model:
[UIHint("Date")]
public DateTime DateProperty { get; set; }
[UIHint("Time")]
public DateTime TimeProperty { get; set; }
The UIHint
attribute name has to match the file name of your template, and UIHint
is in System.ComponentModel.DataAnnotations
, so you will need the appropriate using statement/assembly reference if you don't have it already.
Alternatively, use a TimeSpan
to represent your times - that is what DateTime
returns for its TimeOfDay
property...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With