Doubt with registry edition Asp.net mvc

Asked

Viewed 77 times

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>

Receives the id to search the record:

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 to make the query User registration() :

        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: inserir a descrição da imagem aqui

  • tbuscar.Listarporid(id); it returns something, since it was not assigned to any variable?

  • yes, return, I will add the User Registration Code

  • @model IEnumerable<Generico.Dominio.TB_USUARIO> change to @model Generico.Dominio.TB_USUARIO and on the line tbuscar.ListarPorID(id) put in front a TB_USUARIO us = tbuscar.ListarPorID(id) and in the return View(tbuscar); swap for return View(us);

  • John, your answer is correct, you can post the answer so you can mark. I appreciate the help!

  • I was happier that you solve your problems already put the answer then delete here and the other comments.

2 answers

4

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

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

Just to be clear, this is the part where you define the Model of View:

 @model IEnumerable<Generico.Dominio.TB_USUARIO>
  • Thanks for the help, more on my listing page I have : @model Ienumerable<Generico.Dominio.TB_USUARIO> works normal. So the question.

  • I am already passing a Ienumerable<Generico.Dominio.TB_USUARIO> to the View

  • You’re getting this on View, but on your controller you’re passing CadastroUsuarioAplicacao in this part: var tbuscar = new CadastroUsuarioAplicacao();&#xA; tbuscar.ListarPorID(id);&#xA; return View(tbuscar);

3


From the comments I could see the mistakes:

In your View change from:

@model IEnumerable<Generico.Dominio.TB_USUARIO> 

for

@model Generico.Dominio.TB_USUARIO

No Controller AlteraRegistro do:

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


and in the return View(tbuscar); swap for return View(us);

  • 1

    I’m very happy to find people who waste a few minutes of their time to help, are people like you and so many others who make stackoverflow one of the best sites for learning and sharing knowledge.

  • @itasouza vlw...

Browser other questions tagged

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