Data formatting works on Chrome and does not work on Internet Explorer, or vice versa, with MVC C#

Asked

Viewed 777 times

5

If I wear the model

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd'/'MM'/'yyyy}")]
public DateTime data { get; set; }

Works perfectly on I.E. but doesn’t work on Chrome. When I open the Edit form, instead of the date, it appears dd/mm/yyyy. I can edit and the change is saved. But the value does not load in the edit form when opened.

If I change to

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

There it works perfectly on Google Chrome, but in the I.E. the date appears as 2015-08-10, for example...

How can I make it work equally in both browsers, with the format dd/MM/yyyy?

If I use

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

generates the error: System.Formatexception: The input string was not in an incorrect format.

If I use Html.Textboxfor instead of Html.Editorfor works both... the problem is that it gets time, in format. Ex: "06/15/2015 00:00:00"

If I use

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]

solves the problem in Internet Explorer... but in Chrome it displays dd/mm/yyyy

I don’t know what else to do... someone has an efficient solution?

Thank you!

  • [Displayformat(Applyformatineditmode = true, Dataformatstring = "{0:dd/MM/yyyy}")]

  • You are using Html.EditorFor or Html.TextBoxFor?

  • Take a look at the final HTML being generated, probably the MVC is generating some kind of <input type="date"> which is not supported by Internet Explorer.

  • Vanderson, [Displayformat(Applyformatineditmode = true, Dataformatstring = "{0:dd/MM/yyyy}")] generates the error: System.Formatexception: The input character string was not in an incorrect format.

  • Gypsy, I was using Editorfor... with the tip of Vanderson and using Html.Textboxfor worked on both... the problem is that it got time in the format. Ex: "15/06/2015 00:00:00"

  • Wedney... really has a type=date in the final Html... how can I avoid this?

  • @Felipebulle the attribute type of his field in Chrome is like date? If the answer is yes, this is the problem. The field mask overrides the value when it is not in the format that the field accepts. For example, if the date is 01/02/2016 it must be passed to the field with the attribute type=date in American format 02-01-2016. Maybe in the IE he understands by not understanding the type=dateand interpret as type=text

Show 2 more comments

1 answer

1

Try:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
  • Could you explain the solution better? How does this code answer the question? What does it do?

  • developer033, this alternative solves the problem in Internet Explorer... but in Chrome displays dd/mm/yyyy

Browser other questions tagged

You are not signed in. Login or sign up in order to post.