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

查看:38
本文介绍了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; }

当我想使用以下代码在视图中显示值时:

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

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

问题是日期与其时间值一起显示.我想知道为什么它不考虑 DateType 属性并且只显示没有时间的日期值.我知道我可以为 DateTime 创建一个显示模板,但在除出生日期之外的其他情况下,我想将时间与日期一起显示.如何解决问题?

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?

推荐答案

使用 DisplayFormatAttribute 表示值显示时的格式.此外,您可以创建两个 DisplayTemplate,Date 和 DateTime,并使用 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天全站免登陆