Show Datareader return date in View?

Asked

Viewed 52 times

0

I have a form where I have at 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’m returning the data from a DataReader and when I will make the edition of the registration, the same 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;
        }

1 answer

0

To solve this problem, change the @Html.TextBoxFor for @Html.EditorFor and add the mask 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; }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.