问题描述
我正在C#中编写一个ASP.NET MVC3应用程序,并发现在我的视图中调用 Html.HiddenFor
将呈现一个 DateTime
不同(和不正确),如果我要调用 Html.DisplayFor
。 模型,它的价值从有具有DisplayFormat装饰器,这似乎适用于 Html.DisplayFor
。该属性写成:
[DisplayFormat(DataFormatString ={0:dd / MM / yyyy},ApplyFormatInEditMode = true)]
public DateTime MeetingStartDate {get;组; }
视图显示如下:
@ 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不应该打印美国的日期。
使用TextBoxFor的视图中的替代方法如果指定日期格式和html隐藏属性。
@ Html.TextBoxFor(model => model.CreatedDate,{0:dd / MM / yyyy HH: ss.fff},htmlAttributes:new {@type =hidden})
I'm writing an ASP.NET MVC3 application in C# and have found that calling Html.HiddenFor
in my view will render a DateTime
differently (and incorrectly) to if i was to call Html.DisplayFor
.
The model its taking the value from does have a DisplayFormat decorator and this seems to work for Html.DisplayFor
. The property in question is written as:
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime MeetingStartDate { get; set; }
And the view displays this using:
@Html.DisplayFor(model => model.MeetingStartDate)
@Html.HiddenFor(model => model.MeetingStartDate)
The DisplayFor call will render the date as 16/04/2012
however the HiddenFor will render it as value="04/16/2012 00:00:00"
.
I've tried changing the current culture to set a DateTimeFormat
but this had no effect.
The current culture is en-GB so it shouldn't be printing en-US dates anyway.
An in-view alternative using TextBoxFor will work if you specify the date format and the html hidden attribute.
@Html.TextBoxFor(model => model.CreatedDate, "{0:dd/MM/yyyy HH:mm:ss.fff}", htmlAttributes: new { @type = "hidden" })
这篇关于Html.HiddenFor在ASP.NET中格式DateTime不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!