What is the best way to apply default date formatting to all such fields in an ASP.NET MVC 3 app?
My goals are:
- Display all dates in
d MMM yyyy
format- 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 开发者_JAVA百科picker - Build on top of the
System.ComponentModel.DataAnnotations
infrastructure - Still show bad data values such as
12 Joone 2010
after 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.)
I had a similar problem and solved it something like this (code taken as sample from WebGrid, hopefully it will give you an idea):
@{
var viewDictionary = new ViewDataDictionary<DebugDatesStub>(new DebugDatesStub());
}
...
ModelMetadata.FromLambdaExpression(d => d.DateTimeOffset, viewDictionary).PropertyName,
ModelMetadata.FromLambdaExpression(d => d.DateTimeOffset, viewDictionary).DisplayName,
format: (item) => { return new HtmlString(item.DateTimeOffset.ToString("MMM yyyy hh:mm:ss")); })
...
Render the field on the page with:
@Html.EditorFor(m => m.Date)
Create this code in ~\Views\Shared\EditorTemplates\Date.cshtml
:
@using System.Globalization
@model DateTimeOffset?
@{
var propertyState = ViewData.ModelState[ViewData.ModelMetadata.PropertyName];
var displayValue =
propertyState != null && propertyState.Errors.Any() ? propertyState.Value.AttemptedValue :
ViewData.Model.HasValue ? string.Format("{0:d MMM yyyy}", ViewData.Model.Value) :
string.Empty;
ViewData.ModelState.SetModelValue(
ViewData.ModelMetadata.PropertyName,
new ValueProviderResult(displayValue, displayValue, CultureInfo.CurrentCulture));
}
@Html.TextBoxFor(m => m, new { @class = "behaviour-date-picker" })
精彩评论