Show DataReader return date in View?

0

I have a form where I have in View:

<div class="col-md-2 form-group">
    @Html.LabelFor(x => x.CLIENTEDESDE)
    @Html.TextBoxFor(x => x.CLIENTEDESDE, new { @class = "form-control", type = "date" })
    @Html.ValidationMessageFor(x => x.CLIENTEDESDE)
</div>

I am returning the data of a DataReader and when I am going to do the editing of the register, it is not coming filled.

  private List<TB_CLIENTE> TransformaReaderEmListaObjetos(SqlDataReader reader)
        {
            var retornando = new List<TB_CLIENTE>();
            while (reader.Read())
            {

                TB_CLIENTE tabela = new TB_CLIENTE()
                {
                    IDCLIENTE = reader["IDCLIENTE"] == DBNull.Value ? 0 : Convert.ToInt32(reader["IDCLIENTE"]),
                    NOME = reader["NOME"] == DBNull.Value ? string.Empty : reader["NOME"].ToString(),
                    APELIDO = reader["APELIDO"] == DBNull.Value ? string.Empty : reader["APELIDO"].ToString(),
                    CLIENTEDESDE = reader["CLIENTEDESDE"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(reader["CLIENTEDESDE"])
                };

                retornando.Add(tabela);
            }

            reader.Close();
            return retornando;
        }
    
asked by anonymous 15.06.2016 / 17:34

1 answer

0

To solve this problem, change @Html.TextBoxFor to @Html.EditorFor and add DisplayFormat .

<div class="col-md-2 form-group">
  @Html.LabelFor(x => x.CLIENTEDESDE)
  @Html.EditorFor(x => x.CLIENTEDESDE)
  @Html.ValidationMessageFor(x => x.CLIENTEDESDE)
</div>


[Display(Name = "Cliente desde:")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
[DataType(DataType.Date, ErrorMessage = "Data em formato inválido")]
public DateTime CLIENTEDESDE { get; set; }
    
17.06.2016 / 05:58