In asp.net MVC 5, I have a form that displays data from a DTO object:
public class FieldDTO
{
[DataType(DataType.DateTime)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime StartFieldDate { get; set; }
[DataType(DataType.DateTime)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime StopFieldDate { get; set; }
public String FieldName { get; set; }
[DataType(DataType.DateTime)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime StartJob { get; set; }
[DataType(DataType.DateTime)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime StopJob { get; set; }
}
In my controller class, I have:
public ActionResult Create()
{
var model = new FieldDTO();
model.StartFieldDate = DateTime.Now;
model.StopFieldDate = DateTime.Now;
model.StartJob = DateTime.Now;
model.StopJob = DateTime.Now;
...
...
}
In the view, I have:
<div class="col-md-10">
@Html.EditorFor(model => model.StartFieldDate, new { htmlAttributes = new { @class = "form-control datepicker" } })
@Html.ValidationMessageFor(model => model.DataTurno, "", new { @class = "text-danger" })
</div>
And, I have the same razor code for the other datetime controller. I have added the datepicker class for jQuery UI DatePicker.
In the form, if I click on the "create" button, without modifying any controls, everything works.
When I change one of the last two datetime pickers and then click the "create" button, I get the validation error: The field xxxx must be a date.
I don't figure out what the problem is, because all the controls are generated the same way.
EditorForformodel.StartFieldDateandValidationMessageForforDataTurno?