Aspnet Core - Failed to display date in form

Asked

Viewed 36 times

0

I’m making a system to exercise aspnet core.

In the system I have a record of Transaction, that is already working correctly, and at this time I am making an option to edit a transaction that was registered in the system, when I click edit, a form appears with the old data filled, one of them being the Date, but for some reason I can’t show the date.

I’ve done some checking, being them:

-When I click edit, the item id is passed by the url. In the Transacaomodel model the login id_user and the item id are passed correctly.

-Each variable that is added to the item object in the Chargeback method is correctly added.

But for some reason, when it arrives in the View the variables do not take the values, they remain 0 or empty

follows my code. View

@model TransacaoModel

    <h3>Registrar Transação</h3>

    @{
        var vData = "";
        var vDescricao = "";
        var vTipo = "";
        var vValor = 0;
        var vId = 0;
        var vContaId = 0;
        var vPlanoContaId = 0;

        try
        {
            vId = int.Parse(ViewBag.Registro.Id.ToString());
            vData = DateTime.Parse(ViewBag.Registro.Data.ToString());
            vContaId = int.Parse(ViewBag.Registro.Conta_Id.ToString());
            vValor = double.Parse(ViewBag.Registro.Valor.ToString());
            vPlanoContaId = int.Parse(ViewBag.Registro.Plano_Contas_Id.ToString());
            vDescricao = ViewBag.Registro.Descricao.ToString();
            vTipo = ViewBag.Registro.Tipo.ToString();

        }
        catch
        {

        }
    }

    <form asp-controller="Transacao" asp-action="Registrar">
        <input type="hidden" asp-for="Id" value="@vId" />

        <div class="form-group">
            <label>Data:</label>
            <input type="date" class="form-control" asp-for="Data" value="@vData" />
            <span asp-validation-for="Data" class="text-danger"></span>
        </div>

Controller

[HttpGet]
        public IActionResult Registrar(int? id)
        {
            if (id != null)
            {
                TransacaoModel objTransacao = new TransacaoModel(HttpContextAccessor);
                ViewBag.Registro = objTransacao.CarregarRegistro(id);
            }
            ViewBag.ListaContas = new ContaModel(HttpContextAccessor).ListaConta();
            ViewBag.ListaPlanoContas = new PlanoContaModel(HttpContextAccessor).ListaPlanoConta();
            return View();
        }

Model

public TransacaoModel CarregarRegistro(int? id)
        {
            TransacaoModel item;

            string id_usuario_logado = HttpContextAccessor.HttpContext.Session.GetString("IdUsuarioLogado");
            string sql = "select t.Id, t.Data, t.Tipo, t.Valor, t.Descricao as historico , t.Conta_Id, c.Nome as conta, " +
                        " t.Plano_Contas_Id, p.Descricao as plano_conta from transacao as t inner join conta c " +
                        " on t.Conta_Id = c.Id inner join Plano_Contas as p " +
                        " on t.Plano_Contas_Id = p.Id " +
                        $" where t.Usuario_Id={id_usuario_logado} and t.Id='{id}'";
            DAL objDAL = new DAL();
            DataTable dt = objDAL.RetDataTable(sql);

            item = new TransacaoModel();
            item.Id = int.Parse(dt.Rows[0]["ID"].ToString());
            item.Data = DateTime.Parse(dt.Rows[0]["Data"].ToString()).ToString("dd/mm/yyyy");
            item.Descricao = dt.Rows[0]["historico"].ToString();
            item.Valor = double.Parse(dt.Rows[0]["Valor"].ToString());
            item.Conta_Id = int.Parse(dt.Rows[0]["Conta_Id"].ToString());
            item.NomeConta = dt.Rows[0]["conta"].ToString();
            item.Plano_Contas_Id = int.Parse(dt.Rows[0]["Plano_Contas_Id"].ToString());
            item.DescricaoPlanoConta = dt.Rows[0]["plano_conta"].ToString();
            item.Tipo = dt.Rows[0]["TIPO"].ToString();

            return item;
        }

If that data is insufficient, just let me know that I share more.

My project this here

1 answer

0

Good night,

Put your code in GIT so we can help more easily.

One thing I noticed looking at the code snippet you posted is that in the View it has an empty "Catch":

inserir a descrição da imagem aqui

Remove this Try / catch and see if an error is not occurring on these lines...

Att,

Edson José Martins

  • Eae Edson, sorry for the delay, I made a repository on github, posted the address on the question, see if you can help me

Browser other questions tagged

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