c# - Html.HiddenFor 在 ASP.NET 中错误地格式化 DateTime

我正在用 C# 编写一个 ASP.NET MVC3 应用程序,并发现在我的 View 中调用 Html.HiddenFor 会以不同方式(并且不正确地)呈现 DateTime如果我要调用 Html.DisplayFor.

从中获取值的模型确实有一个 DisplayFormat 装饰器,这似乎适用于 Html.DisplayFor。有问题的属性写成:

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

View 使用以下方式显示:

 @Html.DisplayFor(model => model.MeetingStartDate)
 @Html.HiddenFor(model => model.MeetingStartDate)

DisplayFor 调用会将日期呈现为 16/04/2012 但 HiddenFor 会将其呈现为 value="04/16/2012 00:00:00".

我已尝试更改当前文化以设置 DateTimeFormat 但这没有效果。

当前的文化是 en-GB,因此无论如何都不应该打印 en-US 日期。

最佳答案

如果您指定日期格式和 html hidden 属性,则使用 TextBoxFor 的 View 内替代方案将起作用。

@Html.TextBoxFor(model => model.CreatedDate, "{0:dd/MM/yyyy HH:mm:ss.fff}", htmlAttributes: new { @type = "hidden" })

https://stackoverflow.com/questions/9724713/

相关文章:

c++ - 格式化 C++ 控制台输出

c# - String.Format 参数顺序烦人

c++ - Qt - QPushButton 文本格式

javascript - getHours() 返回不正确的值

swift - 在 Swift 中使用 %@ 格式化字符串

php - 如何使用 PHP 发送 HTML 格式的邮件?

reporting-services - SSRS 格式显示为百分比

haskell - 如何将 Rational 转换为 "pretty"字符串?

c# - 在 DataGridView 列中格式化 TimeSpan

perl - 使用 Perl 进行格式化输出的最佳方式