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 显示没有时间的日期
基础教程推荐
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- WPF 模态进度窗口 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
