设置DataFormatString为MVC3日期时间 [英] Set DataFormatString for MVC3 DateTime

查看:122
本文介绍了设置DataFormatString为MVC3日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有正确的显示日期,使用MVC3一些问题的 的DateTime 的对象。

I have some issue with displaying date correctly, using the MVC3 DateTime object.

在控制器中,我设置的 日期= DateTime.Now

In the controller, I set Date = DateTime.Now.

在视图模型:

[Required(ErrorMessage = "Please enter a date")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd.mm.yyyy}")]
public DateTime Date { get; set; }

在View:

@Html.LabelFor(m => Model.Date, "Date") 
@Html.ValidationMessageFor(m => Model.Date) 
@Html.TextBoxFor(m => Model.DatoForIntervju, new { @class = "datepicker" })

输出:

<input class="datepicker" (...) value="26.09.2011 13:26:16" />

不过,我想的值为 11年9月26日

为什么它不工作?

推荐答案

改变

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd.mm.yyyy}")]

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd.MM.yy}")]

然后,代替textboxfor,用

Then, instead of textboxfor, use

@Html.EditorFor(m => m.Date)

这篇关于设置DataFormatString为MVC3日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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