Doubt with asp.net mvc registry edition

2

I have the structure:

Page where I select the record for editing:

            @if (Model.Count() > 0)
            {
                foreach (var item in Model)
                {
                    <tr>
                        <td>@item.NOME</td>
                        <td>@item.LOGIN</td>
                        <td>@item.ADMINISTRADOR</td>
                        <td><a href="/CadastroUsuario/AlteraRegistro/@item.IDUSUARIO" class="btn btn-primary btn-block">ALTERAR</a></td>
                        <td><a href="/CadastroUsuario/ExcluirRegistro/@item.IDUSUARIO" class="btn btn-danger btn-block">EXCLUIR</a></td>
                    </tr>
                }

            }

        </div>

Get the id to search the registry:

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

            try
            {
                var tbuscar = new CadastroUsuarioAplicacao();
                tbuscar.ListarPorID(id);
                return View(tbuscar);
            }
            catch (Exception)
            {
                TempData["Erro"] = "Erro ao Alterar Registro.";
                return RedirectToAction("ListarRegistro", "CadastroUsuario");
            }

        }

Code for the query User Registration Application ():

        public TB_USUARIO ListarPorID(int id)
        {
            var strQuery = string.Format("select * from tb_usuario where IDUSUARIO = '{0}' ", id);
            using (contexto = new Contexto())
            {
                var retornoDataReader = contexto.ExecutaComandoComRetorno(strQuery);
                return TransformaReaderEmListaObjetos(retornoDataReader).FirstOrDefault();
            }

        }

Page to show the record for editing:

   @model IEnumerable<Generico.Dominio.TB_USUARIO>

    @{
        ViewBag.Title = "Index";
    }


    @Html.Partial("_navbarInterno")
    <br />
    @Html.Partial("_PartialMensagens")

    <br />

Error Screen:

    
asked by anonymous 15.06.2016 / 02:17

2 answers

3

For the comments I could see the errors:

In your View change from:

@model IEnumerable<Generico.Dominio.TB_USUARIO> 

to

@model Generico.Dominio.TB_USUARIO

No Controller AlteraRegistro do:

  

in line tbuscar.ListarPorID(id) put in front a TB_USUARIO us = tbuscar.ListarPorID(id); ;

  

and in return View(tbuscar); change by return View(us);

    
15.06.2016 / 03:16
4

The error message is clear. You are passing an object of type CadastroUsuarioAplicacao and in your View you are expecting a IEnumerable<Generico.Dominio.TB_USUARIO> .

Or you change your View to accept CadastroUsuarioAplicacao or pass IEnumerable<Generico.Dominio.TB_USUARIO> to View .

Just to make it clear, it's in this part that you set the Model of View:

 @model IEnumerable<Generico.Dominio.TB_USUARIO>
    
15.06.2016 / 02:33