ASP.Net Core MVC date input value

一曲冷凌霜 提交于 2019-12-08 04:55:34

问题


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.

This is my Edit.cshtml Code:

<div class="form-group">
    <label asp-for="BestellungsDatum" class="control-label"></label>
    <input asp-for="BestellungsDatum" type="date" class="form- control" value="@Model.BestellungsDatum" /> 
    <span asp-validation-for="BestellungsDatum" class="text-danger"></span>
</div>

This is what it looks like, but there have to be the old value at this position:

When i change the input to type="text", the value is shown but there is no datepicker.. Any solutions on this?


回答1:


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.



来源:https://stackoverflow.com/questions/51041234/asp-net-core-mvc-date-input-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!