MVC Razor setting datetime does not display date

609 views Asked by At

I would like to set the value based on a Tempdata but it is not showing . My TempData value stored is '2012-09-28'. I tested both below but it is not displaying in the datepicker:

Model.cs

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
[DataType(DataType.Date)]
public Nullable<DateTime> bookDate{ get; set; }

MyPage.cshtml

@Html.EditorFor(m => m.myModel.bookDate, new { @Value = "2012-09-28", htmlAttributes = new { @class = "form-control"} })

@Html.EditorFor(m => m.myModel.bookDate, new { @Value =  TempData["myDate"], htmlAttributes = new { @class = "form-control"} })
1

There are 1 answers

0
Yiyi You On

When you use @Html.EditorFor,the value of myModel.bookDate will cover @Value = "2012-09-28".You can try to do like this:

<input id="myModel.bookDate" name="myModel.bookDate" value=@TempData["myDate"] class="form-control" type="date"/>