Use Separate DataHora

2

I have a DataHora field and wanted to use separate, that is, a single field in the database called DateHora of type datetime, and manipulate this field with two editorfor where I saved the date of one editorfor and the time of another editorfor in the same database field:

View:

<div class="form-group">
     @Html.LabelFor(model => model.DataHora, "Liberar encomenda as", htmlAttributes: new { @class = "control-label col-md-2" })
     <div class="col-md-10">
           @Html.EditorFor(model => model.DataHora, new { htmlAttributes = new { @class = "form-control" } }) Horas
     </div>
</div>
<div class="form-group">
     @Html.LabelFor(model => model.DataHora, "Liberar encomenda dia", htmlAttributes: new { @class = "control-label col-md-2" })
     <div class="col-md-10">
           @Html.EditorFor(model => model.DataHora, new { htmlAttributes = new { @class = "form-control" } })
     </div>
</div>

Class:

 public DateTime DataHora { get; set; }
    
asked by anonymous 29.10.2016 / 05:34

1 answer

2

If you want to necessarily use a field in the database, you will need a ViewModel to consolidate date and time.

SeuModel.cs

public DateTime DataHora { get; set; }

SeuViewModel.cs

[DataType(DataType.Date)]
public DateTime Data { get; set; }
[DataType(DataType.Time)]
public TimeSpan Hora { get; set; }

View

<div class="form-group">
     @Html.LabelFor(model => model.Hora, "Liberar encomenda as", htmlAttributes: new { @class = "control-label col-md-2" })
     <div class="col-md-10">
           @Html.EditorFor(model => model.Hora, new { htmlAttributes = new { @class = "form-control" } }) Horas
     </div>
</div>
<div class="form-group">
     @Html.LabelFor(model => model.Data, "Liberar encomenda dia", htmlAttributes: new { @class = "control-label col-md-2" })
     <div class="col-md-10">
           @Html.EditorFor(model => model.Data, new { htmlAttributes = new { @class = "form-control" } })
     </div>
</div>

Controller

model.DataHora = viewModel.Data + viewModel.Hora;

Or, you can define your Model already with the separate fields, there you do not need ViewModel , nor Controller logic above.

    
29.10.2016 / 06:30