Display data tables grouped and separated by headers in an MVC view

I am full C # and MVC noob, so for me it was a fierce fight.

I did a terrible but ultimately successful job of creating a website for the job to show the results of the upcoming primary / local elections.

What I need in the final presentation of my site is to show all race results in the same category. For example, three positions are opened in the city commission, and I need results for all three races on one page. I can display all the results that I want, filtered by category, and in the css table enough that I cannot understand is how I separate this table with subtitles for each individual race.

The data is direct Linq to SQL and is derived from a single view on the server (repositories are what should happen in my next project, if they are not essential for this function).

+3
source share
2 answers

Iโ€™m not quite sure that this is what you need, let me know and I will try to help.

This will not work directly for you, but hopefully you can correctly point to it. I am currently using a custom view model to store this data, and I am actually thinking about getting away from it, as my system is becoming very complex, but it can help you.

I can explain what it means if I need you, (I was noob just a year ago!)

, , .

 <% foreach (var group in Model.GroupBy(item => item.categoryId)) Gives you the inital sort
 { %>
<% foreach (var item in group.Take(1))
{ //Category %>
<%=Html.Encode(item.CategoryName) %>
<% } %>

<% foreach (var item in group)
{ //Indervidual races%>

<%=Html.Encode(item.raceResult) %>
<% } %>


<% foreach (var item in group.Take(1))
{ %>
<!-- Area which happens after each category grouping -->
<% } %>
<% } %>
+4

!! , . Model.groupby - .

, FWIW:

<% 
foreach (var group in Model.GroupBy(item => item.contestNumber)) 
{
    foreach (var item in group.Take(1)) 
    {
%>
        <table width="600">
            <tr>
                <th>
                    <%: item.txtContest %>
                </th>
                <th width="40"></th>
            </tr>
<% 
        foreach (var result in group) 
        { 
%>
            <tr>
                <td>
                    <%: result.Candidate %>
                </td>
                <td>
                    <%: result.voteTotal %>
                </td>
            </tr>

<% 
        } 
%>

        </table>
        <br />
        <br />
        <br />
<% 
    }
} 
%>
+2

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


All Articles