Linq groupby in strongly typed MVC view

How to get IGrouping result for display in view?

I have this query:

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

What is the correct display for a ViewPage ad?

Inherits="System.Web.Mvc.ViewPage<IEnumerable<ProductManual>>"
+3
source share
3 answers

I'm still in the early stages of learning Linq, so I might be wrong, but I think this ViewPage should be something like

Inherits="System.Web.Mvc.ViewPage<IEnumerable<IGrouping<Series, Manual>>>
+4
source

In case of helping others. This is what I tried to accomplish.

Action code

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

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. When the "Key" is a grouped property in the source list.

+3
source

. , .

You can also use ViewData["Products"]to set the collection information that you want to see on the aspx View page.

0
source

Source: https://habr.com/ru/post/1745197/


All Articles