Wednesday, 21 August 2013

DateTime Formatting in MVC4 - DisplayTemplates

DateTime Formatting in MVC4 - DisplayTemplates

Im using MVC4 and have a lot of properties on my model that are of type
DateTime?.
I thought that I could create a DisplayTemplate (inside
Shared\DisplayTemplates) that will format my date as I wish.
However this does not work. It does not output the date in the specified
format. Any help would be appreciated..
Model:
[Display(Name = "Date Submitted")]
[DataType(DataType.DateTime)]
public DateTime? DateSubmitted { get; set; }'
View:
@Html.DisplayFor(m => m.DateSubmitted)
Display Template:
DateTime?:
@model DateTime?
@string.Format("{0:yyyy-MM-dd}", Model)

No comments:

Post a Comment