ASP.NET MVC date are not been displayed View

664 Views Asked by At

Almost working. Only one problem still remains.

This fields have not been displayed in the corresponding iput fields while loading my "View"

[DataType(DataType.Date)]
[Display(Name = "Ínicio do contrato")]
public DateTime? DataInicio { get; set; }

[DataType(DataType.Date)]
[Display(Name = "Fim do contrato")]
public DateTime? DataFim { get; set; }

This model properties are not empty, but don't appear in 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>

I Found out the solution of Entity Frame work Problem.

This is the error message:

An error occurred while processing your request. Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions.

It is caused by ausence of field to represent the Primary Key of register in View while editing register.

His can be solved by putting Hidden input of primary key.

@Html.HiddenFieldFor(model => model.ID)

Doing this, it will fill missing key, and every thing works fine.

3

There are 3 best solutions below

3
Joshua Duxbury On

your DataFormatString includes the seconds {0:hh:mm:ss}

Id also use DateType instead and you can avoid the regex

This will work for what you need

Model

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

or try this regex

"((([0-1][0-9])|(2[0-3]))(:[0-5][0-9])(:[0-5][0-9])?)"
20
Grizzly On

You are not passing validation because as your question states.. you are inputting 18:00 which is Hour and Minute.

In your model, your string is formatted to hh:mm:ss which is hour, minute, and second. So your input is not going to pass validation because 18:00 is missing an extra :00 for seconds.

So, you need to change your input to 18:00:00 to pass validation.

Also, for 24 hour formatting, you need HH:mm:ss.

0
Otavio Camargo On

I discovered the problem. When I specifyed input field type by data annotation, It's not needed any other instruction to validate data like RegexExpression or DisplayFormat. Theese combination of data annotation in my code was causing all problems. By removing them, the user interface works correctly. But as mentioned, changing the input types caused error in Entity Framework when passing values from ViewModel to EntityModel to update data on data base. But, as @Joshua Duxbury mentioned, it might be other problem. Thanks everyone.