日期不会在HTML输入类型日期的Model中显示

Syed Farjad Zia更多

我正在尝试在我的创建和编辑“剃刀视图”上显示日期类型输入...日期选择器工作正常,但是在编辑时确实从模型中获取了值,但日期选择器未显示它...

视图

<div class="form-group form-md-line-input">
    @Html.TextBoxFor(model => model.InitialDate, new { @class = "form-control", type = "date" })
    @Html.ValidationMessageFor(model => model.InitialDate)
    @Html.LabelFor(model => model.InitialDate)
</div>

模型

public Nullable<System.DateTime> InitialDate { get; set; }

我尝试使用,DataAnnotations但同样的事情发生了,日期选择器仅允许我在创建时选择日期,但是在编辑时,它不显示模型中的日期,尽管当我查看呈现的HTML代码时,已设置日期,但控件未显示日期。

HTML视图

<input class="form-control" data-val="true" data-val-date="The field InitialDate must be a date." id="InitialDate" name="InitialDate" type="date" value="3/3/2015 12:00:00 AM">

我还从浏览器的开发人员工具的属性值中删除了时间,以查看它是否有效,但也不起作用...

<input class="form-control" data-val="true" data-val-date="The field InitialDate must be a date." id="InitialDate" name="InitialDate" type="date" value="3/3/2015">

如何在日期选择器中显示来自模型的日期?任何帮助将不胜感激...

用户名

如果您使用此代码生成浏览器HTML5 datepicker实现,则日期格式必须为yyyy-MM-dd(ISO格式)。使用TextBoxFor()它需要

@Html.TextBoxFor(m => m.InitialDate, "{0:yyyy-MM-dd}", new { @class = "form-control", type = "date" })

或者,将以下属性添加到属性中

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public Nullable<System.DateTime> InitialDate { get; set; }

并在视图中使用

@Html.EditorFor(m => m.InitialDate)

请注意,这会添加type="date"属性

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

DatePicker 不会在选择后显示日期

StringFormat 不会在 WPF 中的数据网格内格式化日期(字符串数据类型)

Angular 不会在 HTML 中显示我的数组结果

Font Awesome 不会在 html 中显示

HTML不会在本地显示照片

片段不会在活动中显示

剧情不会在Jupyter中显示

图片不会在wordpress中显示

图像不会在 xamarin 中显示

绘图不会在python中显示

使用 javascript 操作 HTML 输入(日期)类型中的日期

输入类型日期未显示返回日期

“ oc new-app”不会在输入图像中显示端口

如果字段为空,.serialize() 不会在帖子中显示输入的名称

提前输入不会在下拉菜单中显示文本

选择元素不会在其输入字段中显示所选值 - Laravel

自定义帖子类型“单个”不会在wordpress中显示我的代码

角度和输入类型日期不会显示以T格式指定的日期

当数据以小时为间隔时,Anychart 不会在 x 轴上显示日期和时间

Jenkins不会在HTML中显示样式,尽管CSP允许使用样式

该应用程序不会在 file.html 中显示图片 - Django、Python

Foreach循环不会在@ Html.DisplayFor中的项目之间显示空格

html 5 音频标签较低的宽度不会在 chrome 中显示播放按钮

HTML不会在同一行显示按钮

输入值不会在托管Bean中更新

输入文本框不会在ReactJS中重置

Bash脚本不会在输入命令中退出

ReactStrap:在日期类型的输入中显示默认值

从日历控件中选择的日期在运行时不会在文本框中更改