Doubt with Viewbag, loading information for editing

Asked

Viewed 167 times

0

I’m with a mistake, when I go to make a record, I upload the information this way:

//lista 
var tbuscarCategoria = new CadastroCategoriaAplicacao();
var listarCategoria = tbuscarCategoria.ListarTodos();
ViewBag.Categoria = new SelectList(listarCategoria, "IDCATEGORIA", "DESCRICAO");

In the edition of the record, I need to select the category that is in the client register, at this point I have an error:

    public ActionResult AlteraRegistro(int id)
        {
            if (Session["id"] == null)
            {
                return RedirectToAction("Index", "Home");
            }

            try
            {

                var tbuscar = new CadastroClienteAplicacao();
                TB_CLIENTE tbCliente = tbuscar.ListarPorID(id);

                //lista 
                var tbuscarCategoria = new CadastroCategoriaAplicacao();
                var listarCategoria = tbuscarCategoria.ListarTodos();
                ViewBag.Categoria = new SelectList(listarCategoria, "IDCATEGORIA", "DESCRICAO",tbCliente.tbIDCATEGORIA.IDCATEGORIA);

                return View(tbCliente);

            }
            catch (Exception)
            {
                TempData["Erro"] = "Erro ao Alterar Registro.";
                return RedirectToAction("ListarRegistro", "CadastroCliente");
            }
        }

1 answer

0

The problem was occurring because in my class in had an integration with the category table:

        [Display(Name = "Categoría:")]
        [Required(ErrorMessage = "Campo Obrigatório")]
        public TB_CATEGORIA tbIDCATEGORIA { get; set; }

I made the change to only get the IDCATEGORIA of the registration itself:

        [Display(Name = "Categoría:")]
        [Required(ErrorMessage = "Campo Obrigatório")]
        public int IDCATEGORIA { get; set; }

To load the data in the registration view I have:

            var tbuscarCategoria = new CadastroCategoriaAplicacao();
            var listarCategoria = tbuscarCategoria.ListarTodos();
            ViewBag.Categoria = new SelectList(listarCategoria, "IDCATEGORIA", "DESCRICAO");

In view

            <div class="col-md-3 form-group">
                @Html.LabelFor(x => x.IDCATEGORIA)
                @Html.DropDownListFor(x => x.IDCATEGORIA, ViewBag.Categoria as SelectList, new { @class = "form-control" })
                @Html.ValidationMessageFor(x => x.IDCATEGORIA)
            </div>

To load the data in the registration edition I have:

                var tbuscar = new CadastroClienteAplicacao();
                TB_CLIENTE tbCliente = tbuscar.ListarPorID(id);

                //lista 
                var tbuscarCategoria = new CadastroCategoriaAplicacao();
                var listarCategoria = tbuscarCategoria.ListarTodos();
                ViewBag.Categoria = new SelectList(listarCategoria, "IDCATEGORIA", "DESCRICAO", tbCliente.IDCATEGORIA);

That solved the problem.

Browser other questions tagged

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