How to standardize Datetime format for all type fields without using Displayfor or Editorfor in MVC?

Asked

Viewed 291 times

2

In many cases in the application we have to use something like:

@Html.Raw(item.data.ToString("dd/MM/yyyy"))

can’t be DisplayFor nor EditorFor in no case and we have to show the Brazilian date format. How to standardize this at once using the MVC?

  • 4

    Why can’t I use DisplayFor nor EditorFor, where using them is the right way?

  • I thought it would be a mistake to put them inside Webgrid, but I saw that it works.

  • @Tonangelo all right, I’d like to know (if you can) what was the scenario of your problem, because in a way when the application is configured correctly many things are implicit and do not even need to be configured. I’d also like to see in your question that model and also asks if you have set up globalization of your app? If you can answer perhaps many things can be clarified, even if you have positioned yourself in an answer!

  • @Virgilionovic I picked up the system recently, the person who had started left the company and do not know how it generated the classes of the database, no type Datetime of the model has Datatype defined and as the database has many tables, it would be impossible to leave changing everything. The Web.Config consta <globalization enableClientBasedCulture="false" Culture="pt-BR" uiCulture="pt-BR" />, but even if you do not pass formatting continues the American format.

  • 1

    @Tonangelo remove from globalization enableClientBasedCulture="false" do a test or put enableClientBasedCulture="true" will realize that will get formatted correctly. I understand you ... !!! makes this setting. and will have many differences!

  • @Virgilionovic if by chance the client’s computer has OS in another language, this would keep in Portuguese?

  • @Tonangelo what is worth is what is in your application, all who access your site (app) has the language pt-BR or be Portuguese. Yes language is what you set up, changed something?

  • @Virgilionovic changed yes, overall this would almost solve my problem, I still have to set not to display the seconds in all Datetime. But I already appreciate it. This little detail on the Web.Config helped a lot.

  • 1

    @Tonangelo ... !!! realize that if it was configured correctly, even this need not do, but, It is not your fault but who developed first... Guy good luck.

Show 4 more comments

2 answers

3


How to standardize this at once using MVC?

Using DisplayFor and EditorFor. Anything other than that, considering only Razor, is incorrect.

Another thing is about using only the date, not the time (DateTime, in the case, has the two components). In this case, decorate your Model with the type of data ([DataType]) corresponding to date only format (DataType.Date):

using System.ComponentModel.DataAnnotations;

public class SeuModel
{
    ...
    [DataType(DataType.Date)]
    public DateTime Data { get; set; }
    ...
}

Remembering that this generates a <input type="date" />, and not a <input type="text" />, that does not have date formatting by browser default.

  • 1

    The one from Datatype.Date I passed beaten, was making an if (Model.TimeOfDay.Totalseconds == 0) to define if it is just Date. Thanks for the tip!

  • 1

    Taking advantage, see here how the site works to make the most of it. Good studies ;)

3

Hello I find it a better practice to create a component that generates the text on the screen.

Example:

namespace IM.Framework.MVC
{
    using System;
    using System.Text;
    using System.Web.Mvc;

    public static partial class IMHelper
    {
        public static MvcHtmlString IMInputDate(this HtmlHelper html, string Id, string Caption, string PlaceHolder, int Size, DateTime Value)
        {
            var sb = new StringBuilder();
            sb.Append(GeraDiv(new string[] { "input-control", "text", "span" + Size.ToString() }));
            sb.Append(IMLabel(html, Id, Caption));
            sb.Append(String.Format("<input type='text' id='{0}' placeholder='{1}' value='{2}' class='inputPadrao inputDate' />",
                Id, PlaceHolder, Value));
            sb.Append(FechaTag("div"));

            return new MvcHtmlString(sb.ToString());
        }
    }
}

In View use instead of

@Html.Raw(item.data.ToString("dd/MM/yyyy"))

utilize:

@IMHelper.IMInputDate("id_do_campo", "Título", 10, item.data)

Remembering that in Iminputdate I use code that depends on other functions not available here. But the idea is just to give a "Light".

  • The comment of the Gypsy in my question made me think better of my problem, but I will really keep that your answer saved in my files because I liked it a lot. Thank you.

Browser other questions tagged

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