为什么DisplayFormat DataFormatString不工作?(Why is DisplayFormat DataFormatString not working?)
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; }
Yet the markup
<td> @Html.DisplayFor(modelItem => item.MovementDate) </td>
renders the date value as 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; } }
My view:
@model ParkPay.WebTests.Models.WithDateModel @Html.DisplayFor(m => m.TheDate) @section Scripts { @Scripts.Render("~/bundles/jqueryval") }
What gets rendered:
2013/05/25 02:23:37 AM
解决方案
If you can't get it working on the model you could try it on the view.
@Html.TextBoxFor(m => m.ValidFrom, "{0:dd/MM/yyyy}", new {maxlength = 10})
我在我的视图模型的属性如下:
[编辑(假)] [显示(名称=日期)] [DisplayFormat(DataFormatString ={0:YYYY / MM / DD},ApplyFormatInEditMode = TRUE)] 公众的DateTime MovementDate {搞定;组; }
然而,标记
&LT; TD&GT; @ Html.DisplayFor(modelItem =&GT; item.MovementDate) &LT; / TD&GT;
呈现日期值 2013年5月15日12:00:00 AM 。
我是什么做错了吗?我的模型:
公共类WithDateModel { [DisplayFormat(DataFormatString ={0:YYYY / MM / DD})] 公众的DateTime TheDate {搞定;组; } 公共WithDateModel() { TheDate = DateTime.Now; } }
我的观点:
@model ParkPay.WebTests.Models.WithDateModel @ Html.DisplayFor(M = GT; m.TheDate) @section脚本{ @ Scripts.Render(〜/包/ jqueryval) }
什么被渲染:
2013年5月25日上午02点23分37秒
解决方案
如果你不能得到它工作的模型,你可以试试的看法。
@ Html.TextBoxFor(M = GT; m.ValidFrom,{0:DD / MM / YYYY},新的{最大长度= 10})