Date only from TextBoxFor()
MVC4 has solved this problem by adding a new TextBoxFor
overload, which takes a string format parameter. You can now simply do this:
@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" })
<%= Html.TextBoxFor(model => model.EndDate, new { @class = "jquery_datepicker", @Value = Model.EndDate.ToString("dd.MM.yyyy") })%>
[DisplayName("Start Date")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime StartDate { get; set; }
Then:
<%=Html.EditorFor(m => m.StartDate) %>
Or use the untyped helpers:
<%= Html.TextBox("StartDate", string.Format("{0:d}", Model.StartDate)) %>