tags:

views:

1852

answers:

3

Hello,

I'm having trouble displaying the only date part of a datetime into a textbox using TextBoxFor<,>(expression, htmlAttributes).

The model is based on Linq2SQL, field is a DateTime on SQL and in the Entity model.

Failed :

<%= Html.TextBoxFor(model => model.dtArrivalDate, String.Format("{0:dd/MM/yyyy}", Model.dtArrivalDate))%>

Ps : this trick seems to be depreciated, any string value in the object htmlAttribute is ignored.

Failed :

[DisplayFormat( DataFormatString= "{0:dd/MM/yyyy}" )]
public string dtArrivalDate { get; set; }

I would like to store and display the date part only on the details/edit view without the "00:00:00" part.

Any idea please ?

Merry Chrismas from France to all by the way ;-)

+1  A: 

Don't be afraid of using raw HTML.

<input type="text" value="<%= Html.Encode(Model.SomeDate.ToShortDateString()) %>" />
DrJokepu
How would he post it back then? Because date only almost certainly won't get parsed back on the server.
Robert Koritnik
+3  A: 

Or use the untyped helpers:

<%= Html.TextBox("StartDate", string.Format("{0:d}", Model.StartDate)) %>
andersjanmyr
+8  A: 
[DisplayName("Start Date")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime StartDate { get; set; }

Then:

<%=Html.EditorFor(m => m.StartDate) %>
Kevin Craft