ASP.NET MVC displaying date without time(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 显示没有时间的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ASP.NET MVC 显示没有时间的日期


- 在 LINQ to SQL 中使用 contains() 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- 使用 rss + c# 2022-01-01