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.List
1[<>f__AnonymousType4
2[System.Int32,System. String]', but this dictionary requires an item like 'System.Collections.Generic.Ienumerable`1[Controlevisita.Models.USUARIOS_PROCESSO]'.
It’s not even the bootstrap problem, it’s the kind of data you’re sending to the view
– CesarMiguel
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?
– Rabelos
[off-topic] Rabelos, please do not confuse the tags [tag:mvc] with [tag:Asp.net-mvc]
– gmsantos
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>>)– Omni