49

In my model I have the following DataAnnotations on one of my properties

[Required(ErrorMessage = "*")]
[DisplayFormat(DataFormatString = "{0:d}")]
[DataType(DataType.Date)]
public DateTime Birthdate { get; set; }

The required annotation works great, I added the other 2 to try and remove the time. It gets bound to an input in the view using

<%=Html.TextBoxFor(m => m.Birthdate, new { @class = "middle-input" })%>

However whenever the view loads I still get the time appearing in the input box. Is there anyway to remove this using DataAnnotations?

Mathieu
  • 4,449
  • 7
  • 41
  • 60
Gavin
  • 17,053
  • 19
  • 64
  • 110
  • See http://stackoverflow.com/q/1961114/2291 – Jon Adams Jun 12 '12 at 17:43
  • Take a look at... [http://stackoverflow.com/questions/1961114/date-only-from-textboxfor](http://stackoverflow.com/questions/1961114/date-only-from-textboxfor) and [http://stackoverflow.com/questions/726433/datetime-object-in-asp-net-mvc](http://stackoverflow.com/questions/726433/datetime-object-in-asp-net-mvc) – Jim L Jan 04 '10 at 20:26

3 Answers3

91

The [DisplayFormat] attribute is only used in EditorFor/DisplayFor, and not by the raw HTML APIs like TextBoxFor.

Brad Wilson
  • 67,914
  • 9
  • 74
  • 83
  • 6
    I am using MVC4 Razor syntax, the date field is rendered as `@Html.EditorFor(m => m.IssueDate)` and the formatting on the model is applied as `[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}" )]` but the date is still shown as `01/01/0001 12:00:00 AM` – bjan Mar 06 '12 at 06:51
  • 8
    @bjan Did you set the `ApplyFormatInEditMode` property? – Jeremy Holovacs Dec 11 '12 at 15:43
29

As Brad said it dosn't work for TextBoxFor but you'll also need to remember to add the ApplyFormatInEditMode if you want it to work for EditorFor.

[DataType(DataType.Date), DisplayFormat( DataFormatString="{0:dd/MM/yy}", ApplyFormatInEditMode=true )]
public System.DateTime DateCreated { get; set; }

Then use

@Html.EditorFor(model => model.DateCreated)
Paul Johnson
  • 1,417
  • 1
  • 17
  • 26
8

My problem was to set some html attributes (jquery-datepicker), so EditorFor was no option for me.

Implementing a custom helper-methode solved my problem:

ModelClass with DateTime-Property:

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}", ApplyFormatInEditMode = true)]
public DateTime CustomDate{ get; set; }

View with ModelClass as Model:

@Html.TextBoxWithFormatFor(m => m.CustomDate, new Dictionary<string, object> { { "class", "datepicker" } })

Helper-Methode in static helper class:

public static class HtmlHelperExtension {
    public static MvcHtmlString TextBoxWithFormatFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes) {
        ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
        return htmlHelper.TextBox(htmlHelper.ViewData.TemplateInfo.GetFullHtmlFieldName(metadata.PropertyName), string.Format(metadata.DisplayFormatString, metadata.Model), htmlAttributes);
    }
}
Tobias
  • 2,945
  • 5
  • 41
  • 59
  • 2
    You could keep using EditorFor if you just create an Editor template for DateTime that does what you need it to do instead. Editor templates are nice because from the view you can request an Editor without having to know too much about what you're going to get, and you can change all the editors out for different controls at will by just changing the editor templates. – Greg Mar 19 '19 at 17:23