My goals are:
- Display all dates in
d MMM yyyyformat- Default/existing values on first page load
- Anything entered by the user, then round-tripped via the server
- Add
class="behaviour-date-picker"to the input field so that I can show a jQuery date picker - Build on top of the
System.ComponentModel.DataAnnotationsinfrastructure - Still show bad data values such as
12 Joone 2010after a server roundtrip, with the appropriate validation error message
Here’s an example model:
public class DebugDatesStub
{
[DataType(DataType.Date)]
public DateTimeOffset Date { get; set; }
}
The DisplayFormat attribute doesn’t solve my scenario because it doesn’t apply to round-tripped values. (User fills in form, submits it, gets the same form back because of some server-side validation issue – date is still shown in the original format they entered instead of being reformatted.)
Render the field on the page with:
Create this code in
~\Views\Shared\EditorTemplates\Date.cshtml: