为什么 DisplayFormat DataFormatString 不起作用? [英] Why is DisplayFormat DataFormatString not working?
本文介绍了为什么 DisplayFormat DataFormatString 不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的视图模型中有一个属性如下:
I have a property in my view model as follows:
[Editable(false)]
[Display(Name = "Date")]
[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}", ApplyFormatInEditMode = true)]
public DateTime MovementDate { get; set; }
还有标记
<td>
@Html.DisplayFor(modelItem => item.MovementDate)
</td>
将日期值呈现为 2013/05/15 12:00:00 AM
.
我做错了什么?我的模型:
What am I doing wrong? My model:
public class WithDateModel
{
[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}")]
public DateTime TheDate { get; set; }
public WithDateModel()
{
TheDate = DateTime.Now;
}
}
我的观点:
@model ParkPay.WebTests.Models.WithDateModel
@Html.DisplayFor(m => m.TheDate)
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
渲染的内容:
2013/05/25 02:23:37 AM
推荐答案
如果您设置了 [Editable(false)]
,为什么还要使用 ApplyFormatInEditMode
?所有 ApplyFormatInEditMode
所做的就是格式化日期,如果您将日期放在文本框中或用于编辑的东西中,由于上述原因,您可能不会这样做.
Why are you using ApplyFormatInEditMode
if you have set ? All
ApplyFormatInEditMode
does is format the date if you have it in a text box or something for editing, which you probably won't because of the aforementioned.
我能够使用以下方法正确显示日期:
I was able to get the date to display correctly using the following:
[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}")]
public DateTime Date
{
get
{
return DateTime.Now;
}
set
{
Date = DateTime.Now;
}
}
并在视图中(带有结果输出):
and in the view (with resulting output):
@Html.DisplayFor(x => x.Date) // 2013/05/23
<br />
@Model.Date // 23/05/2013 09:57:56
<br />
@Html.DisplayFor(x => Model.Date) // 2013/05/23
希望这会有所帮助.
这篇关于为什么 DisplayFormat DataFormatString 不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文