jason
jason

Reputation: 767

linq groupby in strongly typed MVC View

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>>"

Upvotes: 3

Views: 2052

Answers (3)

jason
jason

Reputation: 767

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.

Upvotes: 3

Junior Mayhe
Junior Mayhe

Reputation: 16411

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.

Upvotes: 0

Nathan Fisher
Nathan Fisher

Reputation: 7941

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>>>

Upvotes: 4

Related Questions