Unsolicited Time Truncation

0

I have a model to write a time used in a task:

public TarefaHoraPendente()
    {
        HorasAcumuladas = new TimeSpan(0, 0, 0);
        Pausada = false;
    }

    public int ID { get; set; }

    [Required(ErrorMessage = "Este campo é obrigatório")]
    [StringLength(10, ErrorMessage = "Qtde. Máx.: 10 caracteres")]
    public string Identificador { get; set; }        

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

    [Required, DataType(DataType.Time)]
    public DateTime HoraInicial { get; set; }

    [DataType(DataType.Time)]
    public DateTime HoraReinicio { get; set; }

    public TimeSpan HorasAcumuladas { get; set; }

    public bool Pausada { get; set; }        
}

When I create a new entity of this model some values are already filled by the Controller:

public ActionResult Create()
    {
        DateTime hoje = new ObtendoDataeHoraLocal();
        TarefaHoraPendente tarefaHora = new TarefaHoraPendente();
        tarefaHora.Data = hoje;
        tarefaHora.HoraInicial = hoje;
        tarefaHora.HoraReinicio = hoje;            

        return View(tarefaHora);
    }

So everyone has the same information, for example: {07/04/2017 10:20:43} with Date, time, minute and SECONDS.

Go to View:

@model Models.Auxiliares.TarefaHoraPendente

@{
    ViewBag.Title = "Projeto";
}

<h2>Iniciar marcação de horas</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        @Html.HiddenFor(model => model.Data)

        <dl class="dl-horizontal">
            <dt>
                @Html.DisplayNameFor(model => model.Data)
            </dt>

            <dd>
                @Model.Data.ToString("dd/MM/yyyy")
            </dd>
        </dl>                

        <div class="form-group">
            @Html.LabelFor(model => model.Identificador, "Identif.(Máx.10 caract.)", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Identificador, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Identificador, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.HoraInicial, "Hora inicial", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.HoraInicial, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.HoraInicial, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Iniciar contagem" class="btn btn-default" />
            </div>
        </div>
    </div>
}

And in View, the StartTime field no longer shows the seconds and I see nothing that indicates this truncation.

Someone knows what I'm doing wrong.

    
asked by anonymous 07.04.2017 / 15:32

1 answer

1

At your View you are passing DateTime complete and EditorFor() is with the Time field. It's the same thing as you doing this:

<input type="time" value="04/07/2017 01:59:00" />

This will not work at all, as you are passing a complete date to a field that only accepts "time."

To solve this, you can use DisplayFormat , the same as you used in the Data property of your Model. However, set the format to Time , like this:

[DisplayFormat(DataFormatString = "{0:HH:mm:ss}", ApplyFormatInEditMode = true)]
[Required, DataType(DataType.Time)]
public DateTime HoraInicial { get; set; }

So you're specifying specifying that you want only the time of date.

Another point is that the input of type time shows / edits only hour and minute by default. To display the seconds, add the step attribute, as shown below:

<p>Sem step</p>
<input type="time" value="01:59:32" />

<p>Com step</p>
<input type="time" value="01:59:32" step="1" />

See the functional example in DotNetFiddle.

    
07.04.2017 / 16:19