ASP.Net Core MVC日期输入值 [英] ASP.Net Core MVC date input value
问题描述
我为EF模型创建了一个编辑视图,但日期选择器有问题. 每当我尝试编辑数据集"时,编辑视图都不会显示旧的日期值.
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.
这是我的Edit.cshtml代码:
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:
当我将输入更改为type ="text"时,会显示该值,但没有日期选择器.对此有任何解决方案吗?
When i change the input to type="text", the value is shown but there is no datepicker.. Any solutions on this?
推荐答案
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
属性(Tag Helpers会正确生成value
属性以实现正确的双向模型绑定,并且您永远不要尝试自己添加它)
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" />
请注意,HTML 5的日期选择器在IE中不受支持,仅在最新版本的FireFox中受支持.如果您想要一个一致的UI,请考虑改用jQuery datepicker.
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.
这篇关于ASP.Net Core MVC日期输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!