views:

34

answers:

2

I'm a complete C# AND MVC noob, so this one's been a bitter struggle for me.

I've done a horrible but ultimately successful job of building a website for work to display the results of the impending primary/local elections.

What I need in the final view of my site is to display all the results of races in the same category. For example, there are three city commission positions open, and I need results for all three races on one page. I can display all the results I want, filtered by category, and in the pretty css table, what I can't seem to figure out is how I split that table up with sub headings for each of the individual races.

The data is direct Linq to SQL and comes out of a single view on the server (repositories are a thing that will have to happen in my next project unless they are essential to this function).

A: 

I'm not quite sure if this is what you are after, do let me know and I'll try to help.

This won't work directly for you, but hopefully might point you in the right way. I'm currently using a custom view model to hold this data, which I'm actually thinking of moving away from as my system is getting very complicated, but it might help you.

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

I have assumed that you want races grouped by category.

 <% 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 -->
<% } %>
<% } %>
optician
A: 

THANK YOU!! That was precisely the information I was looking for. Model.groupby was the break through for me.

Here's the code I used in the final 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 />
<% 
    }
} 
%>
Juspar
Any chance of a cheeky upvote ;)
optician
Marked as answer, can't UP as I don't have any Rep (o;
Juspar