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 显示没有时间的日期


基础教程推荐
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01