I have a Timetable
model which only has two attributes at the moment, an Id
and Date
. It's defined as so:
public class Timetable
{
public int Id { get; set; }
[Required, Column(TypeName = "Date"), DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime Date { get; set; }
}
I then scaffolded some basic CRUD functionality, and thanks to the DisplayFormat
annotation it's allowing me to create timetables with dates in the dd/MM/yyyy
format. However, when I edit an existing timetable, the application is populating the Date
text box with 01/01/2015 00:00:00
(see screenshot).
Is there any way to make my application only use the date, rather than date and time?
@Html.EditorFor()
orTextBoxFor()
?@Html.EditorFor
.DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyInEditMode=true)]
. Note the format string - it will generate the browsers implementation of a datepicker and display it in the culture of the browser. Note also its not supported in all browsers.ApplyFormatInEditMode
, and it didn't create adate
field for my browser's native date picker, I had to add theDataType(DataType.Date)
annotation for that.