开发者

Display data tables grouped and separated by headings on an MVC view

开发者 https://www.devze.com 2023-01-02 10:02 出处:网络
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 impendi

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 开发者_C百科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).


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


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 />
<% 
    }
} 
%>
0

精彩评论

暂无评论...
验证码 换一张
取 消