c# MVC5 @Html.EditorFor datepicker 从不显示值
抱歉打扰,但我已经在这个话题上苦苦思索了 8 个小时了 在这里和那里尝试了所有建议的解决方案,但没有任何变化(好的)。 如果有人可以帮忙...请...
我有一个视图应该显示没有时间的日期选择器
@Html.EditorFor(model => model.OrderDateFilter, new { @class = "form-control datepicker" , @value = "'" + Model.OrderDateFilter.ToString("dd/MM/yyyy") + "'"})
并且显示始终是这样的 错误显示
HTML结果
<input class="text-box single-line" data-val="true" data-val-date="The field OrderDateFilter must be a date." data-val-required="Le champ OrderDateFilter est requis." id="OrderDateFilter" name="OrderDateFilter" type="date" value="09/02/2023">
因此,无论什么值,它都不会显示,除非我单击日期选择器并选择一个新日期,我将表单发布到控制器,值被保留、处理并正确设置回“值”,但从未显示过
我尝试过TextBoxFor 选项 @type='date' 等... 尝试使用 boostrap datepicker 但它显然很丑陋,我永远无法摆脱默认情况下始终显示的小时 00:00:00 格式
这是此日期过滤器的模型部分(如果我不添加 2 个第一个注释,它是相同的)
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime OrderDateFilter { get; set; } //---
Sorry to bother but I've been headbanging on this topic for 8 hours
tried all the suggested solutions here and there and nothing changes (in good).
If someone could help... pleaaaase ...
I have a view supposed to display a datepicker without the hours
@Html.EditorFor(model => model.OrderDateFilter, new { @class = "form-control datepicker" , @value = "'" + Model.OrderDateFilter.ToString("dd/MM/yyyy") + "'"})
and the display is always THIS
error display
HTML result
<input class="text-box single-line" data-val="true" data-val-date="The field OrderDateFilter must be a date." data-val-required="Le champ OrderDateFilter est requis." id="OrderDateFilter" name="OrderDateFilter" type="date" value="09/02/2023">
So whatever the value it is never displayed except when I click the datepicker and choose a new date, I post the form to the controler, value is kept, processed and is correctly set back to 'value' but never shown
I've tried to TextBoxFor options with @type='date', etc...
Tryed use boostrap datepicker but it's obviously ugly and I could never get rid of the hour 00:00:00 format always displayed by default
Here's the Model part for this date filter (if I don't add the 2 first Annotations it's the same)
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime OrderDateFilter { get; set; } //---
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
2022 年 3 月 18 日 - 编辑
模型等中的日期格式..必须是yyyy-MM-dd(因为我不需要小时/分钟..)并且不可能有其他格式。 Razor 显示的日期选择器将选择浏览器默认区域性来显示用户的本地格式,并将在控制器中转换为 yyyy-MM-dd 等。
然后它可以在没有 jQuery datepicker 或其他的情况下工作
18/03/2022 - EDIT
the date format in model etc.. MUST BE yyyy-MM-dd (because I don't need hours/minutes..) and no other format is possible. The datepicker displayed by Razor will pick the browser default culture to display the user's local format and will transate to yyyy-MM-dd in the controler etc..
then it works without jQuery datepicker or others