asp mvc EditorFor DateTime not displaying

荒凉一梦 提交于 2020-12-30 05:10:02

问题


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.


回答1:


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.




回答2:


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.




回答3:


Add Format line in you model

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

Update

OR

Can you change the model property datetime to string. and convert your datetime object to string !!

OR

Or you can format that value in your editor field .for sample like

@Html.TextBoxFor(model => model.FromDate, "{0:d}")



回答4:


Actually until I saw this post, I didn't realise there's "date picker" difference between Chrome and IE, even though I got the sample page layout from the MVC's tutorial.

For someone who doesn't realise the difference, as I did before, please find below the comparison:

enter image description here




回答5:


This is how i do it inside the model

[Display(Name = "Effective Date")]
    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MMM-yyyy}")]
    public DateTime? Effectivedate 
    {
        get { return EffectivedateEdit; }
        set { EffectivedateEdit = value; EffectivedateEdit = value; } 
    }


    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
    public DateTime? EffectivedateEdit { get; set; }



回答6:


I had this exact same problem. If you look at the HTTP Post that was going back to the Controller and the format of the date in the POST it is yyyy-MM-dd.

I changed my data annotation to this and it worked ... Except now the Index list view shows yyyy-mm-dd format. And the Edit shows mm/mm/yyyy in a datepicker control?

Added to Model Date -

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

IMHO EVERY app I've seen since 1980 has used dates and times. Why is this still a mess Mr MS?



来源:https://stackoverflow.com/questions/16843411/asp-mvc-editorfor-datetime-not-displaying

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!