I have a ViewBag in my controller witch contains a List of Linq Items, I created a Foreach Loop in my View to create a table based on the Data in the Viwbag but i get "System.Web.Mvc.SelectListItem" for each item in my loop
Controller Viewbag Code:
foreach (var item in db.Pos.GroupBy(a => a.Pla).Select(p => new
{
Pla = p.Key,
Pdv = p.Select(a => a.Pdv).FirstOrDefault(),
Nombre = p.Select(a => a.Descripcion).FirstOrDefault(),
Rid = p.Select(a => a.Rid).FirstOrDefault(),
Quantity = p.Sum(q => q.Cantidad),
Total = p.Sum(x => x.Total),
Fecha = p.Select(a => a.Fecha).FirstOrDefault()
}))
{
listapop.Add(item.Pdv);
listapop.Add(item.Pla);
listapop.Add(item.Nombre);
listapop.Add(item.Rid);
listapop.Add(item.Quantity.ToString());
listapop.Add(item.Total.ToString());
listapop.Add(item.Fecha.ToString());
}
var grupopopularidad = new SelectList(listapop.ToList());
ViewBag.GroupPops = grupopopularidad;
And mi View Table :
<table>
<thead>
<tr>
<th>Punto de Venta</th>
<th>Platillo</th>
<th>Nombre</th>
<th>Turno</th>
<th>Cantidad</th>
<th>Ingresos</th>
<th>Fecha</th>
</tr>
</thead>
<tbody>
@foreach (var item in ViewBag.GroupPops)
{
<tr>
<th>@item</th>
</tr>
}
</tbody>
</table>
First things first, as @juunas said, the SelectList
is used for creating dropdown menus and probably isn't what you want to use if you are going to output the data to a table. Just send the IEnumerable or a List to the ViewBag instead.
Secondly, you are displaying @item in a th
element while you probably meant for it to be in a td
element.
As for the rest, the Razor view is unable to create all of the td
elements that you need to display your item properties. As a result, you are just getting the object type when you reference the object. You will need to explicitly add them as well as call the properties themselves to get them to show up in the table the way you want.
<table>
<thead>
<tr>
<th>Punto de Venta</th>
<th>Platillo</th>
<th>Nombre</th>
<th>Turno</th>
<th>Cantidad</th>
<th>Ingresos</th>
<th>Fecha</th>
</tr>
</thead>
<tbody>
@foreach (var item in ViewBag.GroupPops)
{
<tr>
<td>@item.Pdv</td>
<td>@item.Pla</td>
<td>@item.Nombre</td>
<td>@item.Rid</td>
<td>@item.Quantity.ToString()</td>
<td>@item.Total.ToString()</td>
<td>@item.Fecha.ToString()</td>
</tr>
}
</tbody>
</table>