ASP.Net Core MVC 日期输入值 [英] ASP.Net Core MVC date input value

查看:31
本文介绍了ASP.Net Core MVC 日期输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为 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" 时,会显示该值但没有 datepicker.. 对此有什么解决方案吗?

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属性(标签助手正确生成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.

这篇关于ASP.Net Core MVC 日期输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆