-1

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>
tereško
  • 58,060
  • 25
  • 98
  • 150
Arturo Martinez
  • 389
  • 7
  • 28
  • GroupPops refers to a SelectList, when you iterate it you get SelectListItems, typically used to populate dropdowns. Perhaps you should just pass the list of string through the viewbag? Or better yet, make the view strongly typed and pass it through the model.. – juunas Mar 01 '16 at 17:38

2 Answers2

1

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>
Jaquez
  • 920
  • 1
  • 13
  • 21
  • Thanks for the anwser it worked nicely, but instead i used a model based of my Viewbag for user recomendations – Arturo Martinez Mar 01 '16 at 23:36
  • Glad I could help! I also agree with using the model instead of the ViewBag. The only reason I didn't suggest it was that I tried not to make too many assumptions about your code. – Jaquez Mar 02 '16 at 14:33
1

General advise, try casting the ViewBag.GroupPops as SelectListfirst. It will help you understand at compile time with what type of object you are working with. Having said that, i totally agree with @Jaquez, use a List<T> for such cases.