编辑条目时的实体框架DateTime格式 [英] Entity Framework DateTime format when editing entry

查看:62
本文介绍了编辑条目时的实体框架DateTime格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Timetable模型,目前只有两个属性,一个IdDate.定义如下:

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格式的时间表.但是,当我编辑现有时间表时,应用程序会在Date文本框中填充01/01/2015 00:00:00(请参见屏幕截图).

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()呈现浏览器日期选择器(<input type="date" ..>),您需要在属性上添加以下属性(注意,ISO格式表示日期将根据浏览器的文化进行显示)

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完全不支持HTML5日期输入-

Note the HTML5 date input is not supported in older browsers, and not at all in FireFox - see comparison here

这篇关于编辑条目时的实体框架DateTime格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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