指定日期时间的格式与数据的注解? [英] Assign format of DateTime with data annotations?

查看:288
本文介绍了指定日期时间的格式与数据的注解?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个属性在我的视图模型:

I have this attribute in my view model:

[DataType(DataType.DateTime)]
public DateTime? StartDate { get; set; }

如果我想显示的日期,或填充一个文本框的日期,我有这些:

If I want to display the date, or populate a textbox with the date, I have these:

<%: Model.StartDate %>

<%: Html.TextBoxFor(m => m.StartDate) %>

每当显示的日期,它的显示如下: 01/01/2011 12:00:00 AM

不过,我想只显示 01/01/2011

有没有应用显示格式数据注解的方法吗?我不希望有去每个实例在哪里显示日期,并添加一些code进行格式化。

Is there a way to apply a display format with data annotations? I don't want to have to go to every instance where I display a date, and add some code to format it.

推荐答案

尝试用它标记:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]

这篇关于指定日期时间的格式与数据的注解?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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