gpt4 book ai didi

asp.net-mvc - 如何在 MVC 2 的双向绑定(bind) View 中格式化日期?

转载 作者:行者123 更新时间:2023-12-01 11:08:47 24 4
gpt4 key购买 nike

假设我有一个访问日期的 View :

<%= Html.TextBoxFor(model => Model.Birthday) %>

如何让我的 View 决定日期的格式?我想使用 common formatting options ,重要的是这是双向的(用于显示和数据输入)。

最佳答案

在您的模型中,您可以使用 System.ComponentModel.DataAnnotations 元数据来指定您希望属性采用的格式。例如

[DisplayFormat(DataFormatString="{0:MM/dd/yyyy}")]
public DateTime Birthday { get; set; }

Then, in your view, use:
<%= Html.EditorFor(model => model.Birthday) %>

不幸的是,常规的 HTML Helpers 不尊重元数据,因此您必须使用 Html.EditorFor。

关于asp.net-mvc - 如何在 MVC 2 的双向绑定(bind) View 中格式化日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2538678/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com