How To Use Html.textboxfor With Input Type=date?
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 HTM
Solution 1:
Try this;
@Html.TextBoxFor(model => model.CreationDate,
new { @type = "date", @Value = Model.CreationDate.ToString("yyyy-MM-dd") })
You have to handle null
when setting the value.
OR
If you can change the Model
you can try this;
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime CreationDate{ get; set; }
and in your view you can use EditorFor
helper.
@Html.EditorFor(model => model.CreationDate, new { @type = "date" })
Solution 2:
Model design:
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime CreationDate{ get; set; }
View Design:
<%: Html.EditorFor(m => m.CreationDate, new { @type = "date" }) %>
Solution 3:
I've done something similar with EditorFor and the boostrap-datepicker library:
<div class="input-prepend date" id="startDate" data-date=@DateTime.UtcNow data-date-format="m/d/yyyy">
<spanclass="add-on"><iclass="icon-calendar"></i></span>@Html.EditorFor(m => m.StartDate)
</div>
Solution 4:
Convert c# Datetime to HTML-5 Date value
<inputtype="date" value="<%= SomeFieldDate.ToString("yyyy-MM-dd")%>"/>
Post a Comment for "How To Use Html.textboxfor With Input Type=date?"