views:

1608

answers:

2

As the question title explains, what is the best way to specify a format which my Views use when displaying values of a DateTime property via the Html.TextboxFor method.

The default display includes the Date and Time where I really only want the Date displayed.

Thanks

+4  A: 

In your model definition add the DisplayFormatAttribute to the DateTime property:

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

Then in your View call:

<%=Html.EditorFor(m => m.Date)%>

If all goes well you should get a TextBox with the value filled in in the format specified in the attribute definition.

Nathan Taylor
I was missing a trailing curly brace in the DataFormatString. Make sure to include it.
Nathan Taylor
One caveat: you can't specify custom HTML attributes (e.g. CSS class) with EditorFor.
jammycakes
A: 

Why this solution works with Html.EditorFor(), but not Html.TextBoxFor()?

Why you don't start another thread for this?!
Zote