Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DisplayFormat attribute's DataFormatString parameter does not work when used with @Html.TextBoxFor()

I have a model with some date and time properties.

The time properties have the following DataAnnotation:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:hh:mm tt}")]

And the date properties have:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]

However these are only respected when I use @Html.EditorFor() and not @Html.TextBoxFor().

My current situation will not allow me to use the EditorFor, so how can I force the TextBoxFor to respect these format strings?

like image 727
keeehlan Avatar asked Jul 23 '13 21:07

keeehlan


Video Answer


1 Answers

You can try the following:

@Html.TextBoxFor(m => m.EndDate, "{0:d MMM yyyy}")

There's also an overload that takes html attributes, so you can set the CSS class, wire up datepickers, etc:

@Html.TextBoxFor(m => m.EndDate, "{0:d MMM yyyy}", new { @class="input-large" })
like image 55
DarthVader Avatar answered Sep 30 '22 18:09

DarthVader