Convert date to ToShortDateString in razor

I have a DateTime field that should appear in the edit window

@Html.EditorFor( model => model.StartDate ) 

how can i use something like this;

 @Html.EditorFor( model => model.StartDate.ToShortDateString() ) 

or any custom function to change the DateTime to view.

+8
razor
Jul 09 2018-11-22T00:
source share
1 answer

Use the [DisplayFormat] attribute in your view model:

 public class MyViewModel { [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)] public DateTime StartDate { get; set; } ... } 

and then, in your opinion, simply:

 @model MyViewModel ... @Html.EditorFor(model => model.StartDate) 
+11
Jan 17 2018-12-12T00:
source share



All Articles