Showing posts with label DataFormatString. Show all posts
Showing posts with label DataFormatString. Show all posts

Friday, March 24, 2017

DateTime DisplayFormat seems to have no effect (MVC5, Razor)

Issue:

I had declared in my model the following

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

  @Html.EditorFor(model => model.AuditDate, new { htmlAttributes = new { @class = "form-control" } })
           
But it was not working!

Then I noted that the Default Displayformat is defined to work with @Html.Display or @Html.DisplayFor and inorder to get it working with Editor controls you need to apply ApplyFormatInEditMode = true.



Source:

https://forums.asp.net/t/1857087.aspx?DateTime+DisplayFormat+seems+to+have+no+effect+MVC4+Razor+

Wednesday, March 22, 2017

MVC attribute DataFormatString

Issue

Needed to format dates in an MVC model.  I wanted to use the attribute decorators for the properties.
I just could not remember the syntax for DataFormatString

  [Display(Name = "Date Notified")]
  [DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy} ")]
  public DateTime? Received_Date { get; set; }

{0:dd MMMM yyyy}    -    gives 24 February 2006
{0:MMM dd}    -    gives Feb 24 (substitue MMM with MMMM for the full month name instead of abbreviation)
{0:dd/MM/yy}    -    gives 24/02/06
{0:dd/MM/yyyy}    -    gives 24/02/2006


Source

https://social.msdn.microsoft.com/Forums/windows/en-US/bb8b1e0e-bd41-4206-9895-4e79463b9bc6/dataformatstring-for-datetime-column?forum=winformsdatacontrols