@ Html.EditorFor for DateTime property remove hh: mm: ss

2
In my model I have a field DateTime called DataReferencia , I formatted with Attributes , however when I am in view hh:mm:ss I just want to date 2/29/2016 .

Model

[Column("sdt_DataReferencia")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
[Display(Name = "Referência 01/mm/aaaa")]
public DateTime DataReferencia { get; set; }

View:

<div class="form-group">
     @Html.LabelFor(model => model.Boletos.DataReferencia, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
    @Html.EditorFor(model => model.Boletos.DataReferencia, new { htmlAttributes = new { @class = "form-control", @Value = Model.Boletos.DataReferencia }, })
    @Html.ValidationMessageFor(model => model.Boletos.DataReferencia, "", new { @class = "text-danger" })
</div>
</div>
    
asked by anonymous 29.02.2016 / 18:28

1 answer

2

Preview

Create in Views\Shared the DisplayTemplates directory.

Inside it, create an empty View named DateTime.cs .

Inside this , place the following:

@model DateTime?

@if (Model.HasValue)
{
    @Convert.ToDateTime(Model).ToString("dd/MM/yyyy")
}

All @Html.DisplayFor() will be displayed without the time.

Editing

Alternative 1: TexBoxFor

Use as follows:

@Html.TextBoxFor(model => model.Cliente.DataReferencia, "{0:dd/MM/yyyy}", new { @class = "form-control datepicker", placeholder = "Data de Referência" })

Alternative 2: Field Date

This uses the input Date native of the browser. It is not always a good alternative because some% s of known browsers are pretty bad.

Change this note:

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

To:

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

Next, add:

[DataType(DataType.Date)]

You do not need to change input .

    
29.02.2016 / 18:43