Date does not display from Model on HTML input type date

前端 未结 1 566
广开言路
广开言路 2020-11-27 07:43

I am trying to display a date type input on my create and edit Razor Views... The date picker works fine but when editing I do get the value from the Model but the date pick

相关标签:
1条回答
  • 2020-11-27 08:12

    If your using this to generate the browsers HTML5 datepicker implementation, the format of the date needs to be yyyy-MM-dd (ISO format). Using TextBoxFor() it needs to be

    @Html.TextBoxFor(m => m.InitialDate, "{0:yyyy-MM-dd}", new { @class = "form-control", type = "date" })
    

    Alternatively, add the following attributes to the property

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

    and in the view use

    @Html.EditorFor(m => m.InitialDate)
    

    Note this adds the type="date" attribute

    0 讨论(0)
提交回复
热议问题