I am creating an MVC5 project which utilizes scaffolding and has an EDM as a model. I want to use server-side code to remove the time portion of my datetime fields rather than parsing it with JQuery.
How do I achieve this?
I am creating an MVC5 project which utilizes scaffolding and has an EDM as a model. I want to use server-side code to remove the time portion of my datetime fields rather than parsing it with JQuery.
How do I achieve this?
Try [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
And if you dont want to validate it use a questionmark after the DateTime property like so public DateTime? Date{ get; set; }
[DisplayName("Date of Birth:")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
[Display(Order = 35)]
public DateTime? CODoB { get; set; }
or you can just use editfor and display for templates here is a link
You can add EditorTemplate in the view folder, you need to create EditorTemplates folder and then create DateTime.cshtml and there you can pass the DateTime that came from Model to the default editor only the date portion like:@Html.EditorFor(x=>x.Date)
If that is not useful for you you need to tell me, where do you need to trim the time portion only in the view or also in database, and if so is your model code first or DataBase first approach