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?
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" })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With