且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ASP.Net Core MVC 日期输入值

更新时间:2023-02-16 12:12:34

type="date" 如果支持,则生成浏览器 HTML-5 日期选择器.规范要求将值格式化为 ISO 格式 (yyyy-MM-dd),然后根据浏览器文化显示.

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.

添加asp-format对值进行格式化,去掉value属性(标签助手正确生成value属性为正确2-way 模型绑定,你永远不应该尝试自己添加它)

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" /> 

请注意,IE 不支持 HTML-5 日期选择器,仅在最新版本的 FireFox 中支持.如果您想要一致的 UI,请考虑使用 jQuery 日期选择器.

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.