Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 91
  • Last Modified:

@Html.EditorFor displays mm/dd/yyyy rather than date

My model has a field, CloseDate, defined as datetime.

        [Display(Name = "Close date")]
        [DataType(DataType.Date)]
        public Nullable<System.DateTime> CloseDate { get; set; }

Open in new window


It's displayed on my Edit page as follows:

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

Open in new window


If I enter or select a date, and click Save, the value is saved to my database. However, when the record is again displayed in Edit, I see the same mm/dd/yyyy placeholder that I did when CloseDate was null; and if I click Save, my originally selected date is saved as null.

Screen shot of CloseDate on Edit.shtml
What can I do to get around this? Have I missed something basic?
0
Bryant Farley
Asked:
Bryant Farley
1 Solution
 
Pawan KumarDatabase ExpertCommented:
Try..

[Display(Name = "Close date")]
[DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:HH:mm MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public DateTime CloseDate { get; set; }

Open in new window

1
 
Bryant FarleyAuthor Commented:
Thank you - had missed something very basic, as suspected.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now