Following viewmodel
used in a view is supposed to display a StartDate as, say 9/30/2015
. But it is displaying as 9/30/2015 12:00:00 AM
. How can I make it display without time while using DataAnnotaion
? I know I can use @Model.StartDate.ToString("MM/dd/yyy")
inside view to display date only. But that would mean you have to do it in every view that is using the following ViewModel
:
ViewModel:
...
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime StartDate { get; set; }
...
UPDATE
The corresponding model class of the above ViewModel
already has the following DataAnnotation
that correctly creates the data type in SQL Server table as Date
; and when you run a query on the corresponding table in SSMS
it correctly displays the StartDate column's data with dates only, say, 9/30/2015 etc.)
Model
...
[DataType(DataType.Date)]
public DateTime StartDate { get; set; }
...
StartDate in the sql db is in fact Date
only. Moreover, if I run a query on SSMS
it correctly returns date only.
[DataType(DataType.Date)]
attribute? – Miramirabeauview
I'm using@Model.StartDate
. Per your suggestion I should be usingDisplayFor
instead - correct? – Declarative@Model.StartDate
is still displaying the date except that it's including default time as well such9/30/2015 12:00AM
. AndVS2017
does not recognize@Model.StartDate.Format("d")
– Declarative