only show date in the View without modifying the model

0

and intentionally formatting and I did not find it, how can I do it

the model is generated by the framework I just have to format it in the view

@model IEnumerable<PROGRAM.Models.Lista>

<table>
   <tr >
       <th><b>Fecha Inicio</b></th>
       <th><b>Fecha Final</b></th>
   </tr>
   @foreach (var item in Model)
   {
       <tr>
           <td>&nbsp; &nbsp; &nbsp;@Html.DisplayFor(modelItem => item.FechaInicio, "{0:dd/MM/yyyy}", new { @class = "form-control"})</td>
           <td>&nbsp; &nbsp; &nbsp;@Html.DisplayFor(modelItem => item.FechaFinal, "{0:dd/MM/yyyy}", new { @class = "form-control"})</td>
           <td>
               <div class="row">
                   <div class="col-xs-4">
                       @Html.ActionLink("Editar", "Edit", new { id = item.Id })
                   </div>
                   <div class="col-xs-4">
                       @Html.ActionLink("Imprimir", "Imprimir", new { id = item.Id })
                   </div>
                   <div class="col-xs-4">
                       @Html.ActionLink("Borrar", "Delete", new { id = item.Id })
                   </div>
               </div>
           </td>
       </tr>
   }
</table>

my attempt

    
asked by 18.01.2018 в 15:37
source

3 answers

0

use @Html.ValueFor ..

<td>@Html.ValueFor(modelItem => item.FechaInicio, "{0:dd/MM/yyyy}")</td>
    
answered by 19.01.2018 / 15:01
source
0

Try assigning the property value with the date formatted directly to the anonymous object of DisplayFor :

@Html.DisplayFor(modelItem => item.FechaInicio, new { value = Model.FechaInicio.ToString("dd/MM/yyyy"), @class = "form-control" })
    
answered by 18.01.2018 в 15:54
0

One way to format dates dynamically without having to write as much code is to create an example sample:

In the Shared directory within your Views folder in your project, create a folder called DisplayTemplates and inside create a view whose name is the same as the one defined in your example model:

assuming we have a Model named Book:

 public class Libro
{
    public int LibroId { get; set; }

    public string Titulo { get; set; }
    public string Autor { get; set; }

    [DataType(DataType.Date)]
    public DateTime FechaPub { get; set; }       
}

as you see in the model DatePub is of type DateTime and likewise the date must be declared in your case that's why it shows you the time in 00:00:00. You can apply the annotation [DataType(DataType.Date)] above so that it is of type Date and this is where the magic comes from. When defining type Date we go to the folder we created in Views / Shared / DisplayTemplates and create a view with the name Date.cshtml with the following content:

/Views/Shared/DisplayTemplates/Date.cshtml

@model DateTime
@Model.ToString("dd MMMM, yyyy")

In this way we have defined a template for Helper @Html.DisplayFor() that works like this:

all @ Html.DisplayFor () that shows a field of type Date will be seen in the form "dd MMMM, yyyy" in any part of your app. I hope it will be useful to you

    
answered by 18.01.2018 в 16:20