Html.HiddenFor ASP.NET中的格式不正确的日期时间 [英] Html.HiddenFor formats DateTime incorrectly in ASP.NET

查看:204
本文介绍了Html.HiddenFor ASP.NET中的格式不正确的日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在C#中的ASP.NET应用程序MVC3并且发现调用 Html.HiddenFor 在我看来,将呈现一个日期时间不同(不正确地)到如果我是叫 Html.DisplayFor

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.

其采取的数值模型确实有DisplayFormat装饰,这似乎为 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; }

和视图显示此使用:

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

该DisplayFor通话将呈现日期 16/04/2012 然而HiddenFor将呈现为值=04/16/2012 00:00:00

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".

我试图改变当前的文化设置 DateTimeFormat ,但这没有影响。

I've tried changing the current culture to set a DateTimeFormat but this had no effect.

当前区域性为en-GB,因此它不应该反正打印的en-US日期。

The current culture is en-GB so it shouldn't be printing en-US dates anyway.

推荐答案

在你的模型,对于MeetingStartDate属性的Get函数可以返回一个格式化过的时间?

In your model, for the Get function of the MeetingStartDate property can you return a formatted DateTime?

该DisplayFormat不会隐藏价值的工作,因为MVC创造的隐藏字段的值可以正确的表格中进行分析,从当前的属性设置中的数据提交操作。

The DisplayFormat is not going to work for hidden values because MVC is creating a value in the hidden field that can be parsed correctly during the Form Submit action from the data that is currently set for the property.

你有什么是MVC预期的操作。

What you have is expected actions by MVC.

这篇关于Html.HiddenFor ASP.NET中的格式不正确的日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆