Format Display Data DisplayFor

5

I have a field in the database, DataHora, and wanted to display on the screen for the user, but in separate fields, being Date and Time.

First displays the Date

@Html.DisplayFor(modelItem => item.DataHora)

Second displays Time

@Html.DisplayFor(modelItem => item.DataHora)

I tried to format using the code below, but it did not work ...

@Html.DisplayFor(modelItem => item.DataHora, {0:dd/MM/yyyy})

Questions?

    
asked by anonymous 01.11.2016 / 03:02

2 answers

5

Html.DisplayFor() is not meant for date and time separation as you want. The method serves well only for the full use of variable information.

Use, instead:

@item.DataHora.ToString("dd/MM/yyyy")

and

@item.DataHora.ToString("HH:mm")
    
01.11.2016 / 03:50
0

I'd rather treat this in view model, example:

    public class MyModel
    {
        public DateTime? DataHora { get; set; }

        public string Data
        {
            get { return DataHora?.ToShortDateString(); }
        }

        public string Hora { get { return DataHora?.ToShortTimeString(); } }
    }
    
13.12.2016 / 20:48