2

I want to format my DateTime field in razor in a clean way.

[MetadataType(typeof(Guestbook_Validation))]
public partial class web_GuestBook
{
    // Partial generated EF
}

[Bind(Exclude="Id")]
public class Guestbook_Validation
{
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime? DateEntered { get; set; }
}

Then

@item.DateEntered

But its still displaying as 05/01/2009 13:28:14

Where am I going wrong?

Matt
  • 3,664
  • 3
  • 33
  • 39

2 Answers2

0

See this answer:

Set DataFormatString for MVC3 DateTime

You have to use EditorFor in the view and use ApplyFormatInEditMode = true in your viewmodel - For textbox. To display only, use DisplayFor

Community
  • 1
  • 1
mnsr
  • 12,337
  • 4
  • 53
  • 79
  • I don't want a textbox I just want to display then text. I tried this anyway and it didn't work. – Matt Oct 16 '11 at 11:59
0

Use Html.DisplayFor(x => x.DateEntered) if you want the display format to be used.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928