ASP.Net MVC Data Formatting

asp.net-mvc, formatting, layout, sql

Solution

Here's a quick sample... I'm assuming a strongly typed model that contains a list of Groups with the corresponding files...

MODEL

public class Groups
{
    public List<Files> GroupFiles { get; set; }
    public String Name{ get; set; }
}

public class File
{
    public int FileId { get; set; }
    public String FileName { get; set; }
    public String FileSize { get; set; }
}

VIEW

<%
foreach(var group in myModel.FileGroups)
{
%>
      <h2><%= group.Name %></h2>
      <table>
<%   
    foreach(var file in group.Files)
    { %>

         <tr>
             <td><%= file.FileID %></td>
             <td><%= file.FileName %></td>
             <td><%= file.FileSize %></td>
         </tr>

    <%
    } %>
</table>
<%
}
%>

Problem

Say I have a database table like the following: ``` FileID | FileName | FileSize | Group ------------------------------------- 1 test.txt 100 Group1 2 test2.txt 100 Group1 3 test3.txt 100 Group2 ``` What would be the best way to display this data with an MVC view in the style of: Group 1 Table Containing Group1 files Group 2 Table containing Group1 files What I am getting it, is when I group the results by Group via a linq to sql query, how can I efficiently display the file lists in sections. Thanks for any input.

Original source