ASP.NET MVC error displaying date and time fields in View

1

Hello I'm having some trouble uploading to View.

In the database I have this data saved:

Start Date: (datetime) 1998-11-16 00: 00: 00,000

DateTime: (datetime): 1998-11-16 00: 00: 00,000

Timetable (time): 09: 00: 00.0000000

Sida Time (time): 18: 00: 00.0000000

ValueHora (money): 50.00

The values of the "TimeTime", "StartTime" and "DateTime" fields are not being displayed in the VIEW.

And the "InputTime" field triggers this error: "Input string was not in correct format"

Model

[DataType(DataType.Currency)]
[Range(0, 100, ErrorMessage = "Valor deve estar entre {1} e {2}")]
[Display(Name = "Valor hora extra")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:C0}")]
public decimal? ValorHoraExtra { get; set; }

[DataType(DataType.Date)]
[Display(Name = "Ínicio do contrato")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime? DataInicio { get; set; }

[DataType(DataType.Date)]
[Display(Name = "Fim do contrato")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime? DataFim { get; set; }

[DataType(DataType.Currency)]
[Range(0, 100, ErrorMessage = "Valor deve estar entre {0} e {1}")]
[Display(Name = "Valor hora")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:C0}")]
public decimal? ValorHora { get; set; }

[DataType(DataType.Time)]
[Display(Name ="Horário de entrada")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = @"{0:HH:mm}")]
public TimeSpan? HorarioEntrada { get; set; }

[DataType(DataType.Time)]
[Display(Name = "Horário de saída")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = @"{0:HH:mm}")]
public TimeSpan? HorarioSaida { get; set; }

View

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

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

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

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

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

Another thing in the browser, the date fields are not capturing the value when you select the date in the calendar. This is in Chrome. In Firefox, the calendar does not exist.

What is wrong with the code for these errors to occur?

    
asked by anonymous 15.07.2016 / 15:56

1 answer

2

This will not work here:

[DataType(DataType.Date)]
[Display(Name = "Ínicio do contrato")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime? DataInicio { get; set; }

[DataType(DataType.Date)]
[Display(Name = "Fim do contrato")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime? DataFim { get; set; }

For [DataType(DataType.Date)] , DisplayFormat must be yyyy-MM-dd , by specification of <input type="date"> , following RFC3339 :

[DataType(DataType.Date)]
[Display(Name = "Ínicio do contrato")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime? DataInicio { get; set; }

[DataType(DataType.Date)]
[Display(Name = "Fim do contrato")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime? DataFim { get; set; }

For financial value, DataFormatString is also wrong:

[DataType(DataType.Currency)]
[Range(0, 100, ErrorMessage = "Valor deve estar entre {0} e {1}")]
[Display(Name = "Valor hora")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:C0}")]
public decimal? ValorHora { get; set; }

Should be:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:F2}")]
public decimal? ValorHora { get; set; }

But I'd rather use @Html.TextBoxFor along with JS jQuery Mask Money , whose visual result gets better, so I do not use [DisplayFormat] for financial field.

For time fields, DateFormatString is lost using its format:

[DataType(DataType.Time)]
[Display(Name ="Horário de entrada")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = @"{0:HH:mm}")]
public TimeSpan? HorarioEntrada { get; set; }

[DataType(DataType.Time)]
[Display(Name = "Horário de saída")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = @"{0:HH:mm}")]
public TimeSpan? HorarioSaida { get; set; }

Switch to:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:hh\:mm}")]
public TimeSpan? HorarioSaida { get; set; }

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:hh\:mm}")]
public TimeSpan? HorarioEntrada { get; set; }

Regarding support for these fields in Firefox, Mozilla apparently does not accept the date and time field specifications (sponsored by Google), so you'll have to do the inclusion of Webshim in your project to have these fields also in Firefox.

    
23.08.2016 / 21:38