Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp mvc EditorFor DateTime not displaying

I am trying to display a datetime field retrieved froma database in my razor file with the following:

 @Html.EditorFor(model => model.RequestDate);

I know with 100% certainty the RequestDate is not null, as I examined the model that was passed in the view, and it was the date from the database. However, the datetime textbox still displays "mm/dd/yyyy". I don't know why it is not display the date. Any ideas? Thank you.

Here is my model:

      [Display(Name = "Time")]
    [DataType(DataType.Date)]
    public DateTime RequestDate { get; set; }

EDIT:

   [HttpGet]
    public ActionResult DispatchResponseIndex()
    {
        DispatchResponseModel model = new DispatchResponseModel();       


        //if the users session id isnt null then we know they have data related to this form
        object UserID = Session["TestID"];
        if (UserID == null)
        {
            return View(model); //default view               
        }
        else
        {
            UserID = Session["TestID"];
        }

        LoadDB(model, (Guid)UserID);
     //looking at the model here the RequestDate has a valid date of  //{5/24/2013 12:00:00 AM}

        return View(model); 
    }

EDIT 2:

If I use @Html.TextBox("asdasd",Model.RequestDate); Then it will display the saved date in string format, but I need to use the built in date editor for the text box.

like image 661
John Edwards Avatar asked May 30 '13 18:05

John Edwards


2 Answers

This does seem to cause a lot of confusion and it annoyed me!

If you use DataType.Date you need to set it like this:

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]

Chrome's date picker uses the local computer's short date format. Even setting the lang tag is ignored; here is an example using Chrome and <html lang="en-GB">:

enter image description here

Chrome's date picker also uses the local computer's locale. On page load, the date will be populated (if set) and the user can select a new date:

enter image description here

Note: If you change the computer's locale you must restart Chrome completely to see the locale change.

like image 106
Piotr Kula Avatar answered Sep 20 '22 16:09

Piotr Kula


FINAL

I'm an idiot. I had no idea Chrome had a date picker that it added. I thought it was something built into MVC. I need to use an actual jquery date picker. Thanks everyone for all the info.

like image 43
John Edwards Avatar answered Sep 18 '22 16:09

John Edwards