views:

109

answers:

3

How do i get an IGrouping result to map to the view?

I have this query:

var groupedManuals = manuals.GroupBy(c => c.Series);
return View(groupedManuals);

What is the proper mapping for the ViewPage declaration?

Inherits="System.Web.Mvc.ViewPage<IEnumerable<ProductManual>>"
A: 

I am still in the early stages of learning Linq so I may be wrong but I think that that ViewPage will need to be something like

Inherits="System.Web.Mvc.ViewPage<IEnumerable<IGrouping<Series, Manual>>>
Nathan Fisher
That was it. for whatever reason, i just couldn't pull that out of my head.
jason
A: 

The view must be the same type of the page. You would have to convert the output to the same type used by your view page.

You could also use a ViewData["Products"] to set the collection information you want to be visible on your View aspx page.

Junior Mayhé
A: 

In the instance of helping others out. This is what I was attempting to accomplish.

The action code

var groupedManuals = manuals.GroupBy(c => c.Series);
return View(groupedManuals);

The View

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<IGrouping<string, ProductManualModel>>>" %>
 <% foreach (var item in Model)
           { %>
        <div class="manual-category">
            <h5>
                <%= Html.Encode(item.Key) %> Series</h5>
            <ul>
                <% foreach (var m in item)
                   { %>
                <li><a href="<%=Url.ManualLink(m.Id) %>" class="model-manuals">
                    <%= m.Id %></a> </li>
                <% } %>
            </ul>
    </div>

<% } %>

IGrouping is a list of lists so to speak. With the "Key" being the grouped property in the source list.

jason