ASP.Net Core MVC date input value

后端 未结 2 574
礼貌的吻别
礼貌的吻别 2021-01-05 06:51

I created a edit view for a EF Model and have a problem with the datepicker. Whenever i try to edit a `dataset, the edit view don\'t show me the old date values.

Thi

相关标签:
2条回答
  • 2021-01-05 07:28

    Just like to add an addition to the accepted answer:

    <input asp-for="BestellungsDatum" type="date" class="form-control" />
    

    Rather than adding asp-format to the view, add it to the data Model or ViewModel so you don't have to then add it to every view that uses it:

    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
    public DateTime BestellungsDatum { get; set; }
    
    0 讨论(0)
  • 2021-01-05 07:33

    type="date" generates the browsers HTML-5 datepicker if supported. The specifications require that the value be formatted in ISO format (yyyy-MM-dd) which is then displayed in accordance with the browsers culture.

    Add the asp-format to format the value, and remove the value attribute (Tag Helpers correctly generate the value attribute for correct 2-way model binding and you should never attempt to add it yourself)

    <input asp-for="BestellungsDatum" type="date" asp-format="{0:yyyy-MM-dd}" class="form-control" /> 
    

    Note the HTML-5 datepicker is not supported in IE, and only in recent versions of FireFox. If you want a consistent UI, then consider using a jQuery datepicker instead.

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