ASP.NET MVC无时间显示日期 [英] ASP.NET MVC displaying date without time

查看:146
本文介绍了ASP.NET MVC无时间显示日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的模型字段点缀于以下方式:

I have my model field decorated in the following way:

[DataType(DataType.Date)]
[Display(Name = "Date of birth")]
public string DateOfBirth { get; set; }

当我想用下面的code,以在视图中显示的值:

When I want to display the value in the view using the following code:

<%: Html.DisplayFor(m => m.DateOfBirth) %>

的问题是,该日期是用它的时间价值一起显示。我不知道为什么它没有考虑DateType属性的考虑,只显示日期值没有时间。我知道我可以为日期时间,但在其他情况下,创建一个显示模板比出生日期我想与日期显示在一起的时间。如何解决这个问题呢?

The problem is that the date is displayed together with its time value. I wonder why it does not take DateType attribute into consideration and displays only the date value without time. I know that I may create a display template for DateTime but in other cases than date of birth I want to show time together with date. How to solve the problem?

推荐答案

使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.displayformatattribute.aspx\"相对=nofollow> DisplayFormatAttribute 指示显示值时的格式。此外,您还可以创建两个DisplayTemplates,日期和日期时间,并使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.uihintattribute.aspx\"相对=nofollow> UIHintAttribute 指定模板

Use DisplayFormatAttribute to indicate format when value is displayed. Also, you could create two DisplayTemplates, Date and DateTime, and use UIHintAttribute to specify template

这篇关于ASP.NET MVC无时间显示日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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