在 HTML 输入类型日期上不从模型显示日期 [英] Date does not display from Model on HTML input type date

查看:20
本文介绍了在 HTML 输入类型日期上不从模型显示日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

查看

@Html.TextBoxFor(model => model.InitialDate, new { @class = "form-control", type = "date" })@Html.ValidationMessageFor(model =>model.InitialDate)@Html.LabelFor(model =>model.InitialDate)

模型

public Nullable初始日期 { 获取;放;}

我尝试过使用 DataAnnotations 但同样的事情发生了日期选择器只允许我选择创建时的日期但是在编辑时它不显示模型中的日期虽然当我查看时在呈现的 HTML 代码中,正在设置日期值,但控件不显示它.

HTML 视图

"

<input class="form-control" data-val="true" data-val-date="InitialDate 字段必须是日期."id="InitialDate" name="InitialDate" type="date" value="3/3/2015 12:00:00 AM">

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

"

<input class="form-control" data-val="true" data-val-date="InitialDate 字段必须是日期."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初始日期 { 获取;放;}

并在视图中使用

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

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

I am trying to display a date type input on my create and edit Razor Views... The date picker works fine but when editing I do get the value from the Model but the date picker does not display it...

View

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

Model

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

I have tried using the DataAnnotations but the same thing happens the date picker only allows me to select the date on the create but when editing it does not display the date from the model although when I looked at the HTML Code that is rendered, the value for the date is being set but the control does not display it.

HTML View

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

I also removed the time from attribute value from the browser's developers tools to see if it works, but it didn't work either...

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

How can I Display the Date from the Model in the date picker? Any help will be appreciated...

解决方案

If your using this to generate the browsers HTML5 datepicker implementation, the format of the date needs to be yyyy-MM-dd (ISO format). Using TextBoxFor() it needs to be

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

Alternatively, add the following attributes to the property

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

and in the view use

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

Note this adds the type="date" attribute

这篇关于在 HTML 输入类型日期上不从模型显示日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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