Reputation: 1
I am having issue displaying (C#) DateTime / (LocalDB) Date type correctly in my front-end using a code-first Entity Framework approach.
StudentDetailsDTO
model class:
public int ID { get; set; }
public string Name { get; set; }
public string LastName { get; set; }
[Column(TypeName = "Date"), DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime BirthDate { get; set; }
public string SID { get; set; }
public string Course { get; set; }
public string GroupName { get; set; }
public string Specialty { get; set; }
The column BirthDate
works correctly. It returns date (without time) in the provided format as a form of replacement for DateTime
in the table.
However, in the front-end it is still displayed along with the T attribute in Details table for particular student, as if Date
type in the column had no effect.
If I were to use string
data type instead, there would be no issues. However, I am trying my best to stick to appropriate data types convention.
Do I need to change something in the controller? HTML view? Or maybe in Knockout.js? I've tried some solutions provided here on Stackoverflow, but they either didn't work or I had trouble implementing them.
I am a complete newbie and lost. Such a little change, so many problems. I would be grateful for every possible hint or solution. Thank you for your time.
Upvotes: 0
Views: 4624
Reputation: 6332
you must decorate BirthDate with DataType attribute to make it work.
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime BirthDate { set; get; }
Upvotes: 1
Reputation: 16
It depends on how you are rendering the Date.
In razor you can use:
@Html.DisplayFor(model => model.BirthDate)
or if you are using Knockout to render the date, you could consider moment.js and you'd just replace the date with the birth date from you View Model.
moment('2013-01-01T00:00:00.000').format('YYYY-MM-DD')
Upvotes: 0