为什么DisplayFormat DataFormatString不工作? [英] Why is DisplayFormat DataFormatString not working?

查看:662
本文介绍了为什么DisplayFormat DataFormatString不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的视图模型的属性如下:

  
[显示(名称=日期)]
[DisplayFormat(DataFormatString ={0:YYYY / MM / DD},ApplyFormatInEditMode = TRUE)]
公众的DateTime MovementDate {搞定;组; }

然而,标记

 < TD>
    @ Html.DisplayFor(modelItem => item.MovementDate)
< / TD>

呈现日期值 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})

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不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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