Error sending data to View

Asked

Viewed 857 times

0

Query to bring users.

public IEnumerable PopulaTabelaUsuario()
{
     var banco = new DbBancoContext();
     var listaUsuarios = (from l in banco.USUARIOS_PROCESSO 
         select new{ l.ID_USUARIO, l.NOME_USUARIO }).ToList();
     return  listaUsuarios;
} 

My controller.

public ActionResult AreaPrincipal()
{
    var lista = new ConsultasNecessarias();
    var usuarios = lista.PopulaTabelaUsuario();
    //var r = new DbBancoContext();
    //var s = r.USUARIOS_PROCESSO.Select(m=>m.NOME_USUARIO).ToList();
    return View(usuarios);           
}

My View.

@model IEnumerable<ControleVisita.Models.USUARIOS_PROCESSO>    
@{
    ViewBag.Title = "Área Principal";        
}

@section Menu{        
}
<table class="table table-hover">
    <thead>
        <tr>
            <th>Id Usuário</th>
            <th>Nome</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>@Html.DisplayFor(modelItem => @item.ID_USUARIO)</td>
                <td>@Html.DisplayFor(modelItem => @item.NOME_USUARIO)</td>
            </tr>          
        }
    </tbody>
</table>

But when I run it presents the following error.

The template item inserted in the dictionary is from type'System.Collections.Generic.List1[<>f__AnonymousType42[System.Int32,System. String]', but this dictionary requires an item like 'System.Collections.Generic.Ienumerable`1[Controlevisita.Models.USUARIOS_PROCESSO]'.

  • 1

    It’s not even the bootstrap problem, it’s the kind of data you’re sending to the view

  • I agree Cesarmiguel put only because there is the webgrid but did not want to use. I would like to know how to correct the question of data types?

  • [off-topic] Rabelos, please do not confuse the tags [tag:mvc] with [tag:Asp.net-mvc]

  • 1

    The problem is that you declared in the view that you were going to pass a model of type USUARIOS_PROCESSO. Your controller is actually passing an anonymous class that contains the data you selected in the query (such new {...}). Solution sera either change the query to return such USUARIOS_PROCESSO or change the View with the type sent (as I doubt that to put an anonymous type, I would create a List<Keyvaluepair<int,string>>)

1 answer

1


Using the select new, a new object of the anonymity type will be created as a result of the consultation.

You cannot return an anonymous Ienumerable (IEnumerable<AnonymousType>) because the type to be returned is not known.

Try to modify your method Populatabelausuario() query to return IEnumerable<USUARIOS_PROCESSO>.

public IEnumerable<USUARIOS_PROCESSO> PopulaTabelaUsuario()
{
    var banco = new DbBancoContext();
    IEnumerable<USUARIOS_PROCESSO> listaUsuarios = (
        from l in banco.USUARIOS_PROCESSO
        select new USUARIOS_PROCESSO { l.ID_USUARIO, l.NOME_USUARIO }
    ).ToList();
    return  listaUsuarios;        
}

Browser other questions tagged

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