Why is DisplayFormat DataFormatString not working?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 12:44:23

问题


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

回答1:


Why are you using ApplyFormatInEditMode if you have set [Editable(false)]? 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

Hope this helps.




回答2:


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})




回答3:


Since You want to exclude the time to get Only Date: At Model:-

 [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy/MM/dd}"]
   public DateTime TheDate { get; set; }

At Views:-

    @Html.DisplayFor(model=> model.TheDate)
    @Html.JQueryUI().DatepickerFor(model => model.TheDate)

The tutorial of the following link may help you.It works for me.

http://ilyasmamunbd.blogspot.com/2014/02/jquery-ui-datepicker-popup-calendar.html




回答4:


In my case a teammate had defined a global display template (aka ~\Views\Shared\DisplayTemplates\DateTime.cshtml) that I didn't realize took precedence over my [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")] on my model property.

The solution was to move my format string to new shared template, so the attribute changes to:

// see ~\Views\Shared\DisplayTemplates\DateMyFormat.cshtml for formatting
[UIHint("DateMyFormat")]
public DateTime MovementDate { get; set; }



回答5:


You have to annotate the type as Date

[DataType(DataType.Date)]



回答6:


This work for me:

In the model:

using System.ComponentModel.DataAnnotations;

namespace Athlete.Models
{
    public class Foo
    {

        [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
        public DateTime SignDate
        {
           get;
           set;
        }
    }
}

And in the view:

<td style="text-align:left;">@Html.DisplayFor(modelItem => item.SignDate)</td>



回答7:


You need the var name in the Display Name:
[Display(Name = "MovementDate")]
public DateTime MovementDate { get; set; }

[Editable(false)]
[Display(Name = "MovementDate")]
[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}", ApplyFormatInEditMode = true)]
public DateTime MovementDate { get; set; }


来源:https://stackoverflow.com/questions/16697872/why-is-displayformat-dataformatstring-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!