编辑条目时的实体框架日期时间格式 [英] Entity Framework DateTime format when editing entry
问题描述
我有一个 Timetable
模型,它目前只有两个属性,一个 Id
和 Date
.定义如下:
I have a Timetable
model which only has two attributes at the moment, an Id
and Date
. It's defined as so:
public class Timetable
{
public int Id { get; set; }
[Required, Column(TypeName = "Date"), DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime Date { get; set; }
}
然后我搭建了一些基本的 CRUD 功能,多亏了 DisplayFormat
注释,它允许我使用 dd/MM/yyyy
格式的日期创建时间表.但是,当我编辑现有时间表时,应用程序会使用 01/01/2015 00:00:00
填充 Date
文本框(参见屏幕截图).
I then scaffolded some basic CRUD functionality, and thanks to the DisplayFormat
annotation it's allowing me to create timetables with dates in the dd/MM/yyyy
format. However, when I edit an existing timetable, the application is populating the Date
text box with 01/01/2015 00:00:00
(see screenshot).
有什么办法可以让我的应用程序只使用日期,而不使用日期和时间?
Is there any way to make my application only use the date, rather than date and time?
推荐答案
为了使用 EditorFor 呈现浏览器日期选择器 (
你需要属性上有以下属性(注意ISO格式表示日期会按照浏览器文化显示)<input type="date" ..>
)()
In order to render the browsers datepicker (<input type="date" ..>
) using EditorFor()
you need the following attributes on the property (note the ISO format means the date will be displayed in accordance with the browser culture)
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode=true)]
[DataType(DataType.Date)]
public DateTime Date { get; set; }
在视图中
@Html.EditorFor(m => m.Date)
请注意,旧浏览器不支持 HTML5 日期输入,FireFox 根本不支持 - 在这里查看对比
Note the HTML5 date input is not supported in older browsers, and not at all in FireFox - see comparison here
这篇关于编辑条目时的实体框架日期时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!