Well, I have a little big problem here. I make the query in the database using Lambda
, and I use the GroupBy
tag, the query is bringing objects correctly, the problem is that I can not show in the view. gives an error when I use IEnumerable<>
and when I try to use List
of error tbm. but if I do not use GroupBy
, it shows on screen, using IEnumerable<>
, it follows code:
Controller :
public ActionResult RelatorioDatadois(DateTime? dataInicio, DateTime? dataFim)
{
ViewBag.dataInicial = dataInicio;
ViewBag.dataFinal = dataFim;
if (dataInicio == null && dataFim == null)
{
return View();
}
else
{
var vrDb = db.VrDb.Where(v => v.DataSolicitacao >= dataInicio && v.DataSolicitacao <= dataFim && v.Situacao == Situacao.Finalizado).GroupBy(v => v.Veiculo.Id).ToList();
var data = dataInicio;
var dataF = dataFim;
return View(vrDb);
}
}
View :
@model IEnumerable<GaragemModel.Vr>
<div class="panel-heading">
<ul class="list-inline">
<li> <h4>Período:</h4></li>
<li>@ViewBag.dataInicial Até</li>
<li> @ViewBag.dataFinal</li>
</ul>
</div>
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>
Placa
</th>
<th>
Descrição
</th>
<th>
Combustivel Gasto
</th>
<th>
Km Percorrido
</th>
<th>
Gasto
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr class="odd gradeX">
<td>
@Html.DisplayFor(modelItem => item.Veiculo.Placa)
</td>
<td>
@Html.DisplayFor(modelItem => item.Veiculo.Descricao)
</td>
<td>
@Html.DisplayFor(modelItem => item.Mv.Consumo) Litros
</td>
<td>
@Html.DisplayFor(modelItem => item.Mv.QuilometrosPercorridos) Km
</td>
<td>
R$ @ViewBag.TotalValorEmDinheiro
</td>
</tr>
}
</tbody>
</table>
The error When I use IEnumerable<>
:
The template item entered in the dictionary is from type 'System.Collections.Generic.List
1[System.Linq.IGrouping
2 [System.Int32, GaragemModel.Vr]]', but this dictionary requires an item of type 'System.Collections.Generic.IEnumerable'1 [GaragemModel.Vr]'.