How to set value in HTML5 date field from Asp.Net MVC Razor?
I found a similar question here , but it doesn't work for my case. Take a look at the screenshot below. When I process the data in the edit form, all the data has been correctly set to the controls except for the highlighted date field. When I inspect the element, I find that the value is there, but for some reason it doesn't appear in the field.
Here is the code I used to set the value to this date field. I tried the format "yyyy-MM-dd and " MM / dd / yyyy but none worked.
<td>Joining Date</td>
<td>
@{
if(Model.JoiningDate != null)
{
<input id="txtJoiningDate" type="date" style="width: 100%;" value="@String.Format("{0:MM/dd/yyyy}",Model.JoiningDate.Value)" />
}
else
{
<input id="txtJoiningDate" type="date" style="width: 100%;" />
}
}
</td>
Edit
+3
source to share
5 answers
using Html helper
@Html.TextBox("txtJoiningDate", Model.JoiningDate.Value ,"{0:MM/dd/yyyy}")
In the model
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
public Nullable<System.DateTime> JoiningDate{ get; set; }
This works for me
+2
source to share