Html.HiddenFor ASP.NET中的格式不正确的日期时间

编程入门 行业动态 更新时间:2024-10-25 18:31:00
本文介绍了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中的格式不正确的日期时间

本文发布于:2023-11-12 09:39:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581142.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不正确   日期   格式   时间   HiddenFor

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!