This question already has an answer here:
- Date only from TextBoxFor() 17 answers
I want to implement in my code. To that end, I have the following:
@Html.TextBoxFor(model => model.CreationDate, new { @type = "date" })
This code generates the following HTML:
<input data-val="true" data-val-date="The field CreationDate must be a date."
data-val-required="The CreationDate field is required."
id="CreationDate" name="CreationDate" type="date"
value="09/05/2012 15:02:19">
The value does not show up on the web page because type="date" expects data in YYYY-MM-DD format.
I've tried formatting the date, but that blows up, of course.
@Html.TextBoxFor(model => model.CreationDate.ToString("YYYY-MM-DD"),
new { @type = "date" })
How do I use TextBoxFor method to properly display the construct? Or should I be using some other method (I already tried EditorFor but failed)?
CreationDate
is of type DateTime.
Model design:
View Design:
I've done something similar with EditorFor and the boostrap-datepicker library:
Try this;
You have to handle
null
when setting the value.OR
If you can change the
Model
you can try this;and in your view you can use
EditorFor
helper.Convert c# Datetime to HTML-5 Date value