views:

65

answers:

1

Hi. I have 3 classes: class R{ string NameR;}

class A{ string NameA;}

Class RP

{

R objR;

A objA;

bool result;

}

I use NHibernate so in DB I have a table RP:

[Id] [int] IDENTITY(1,1) NOT NULL,

[Result] [bit] NULL,

[RId] [int] NULL,

[AId] [int] NULL

I need to display in my view a matrix like this:

__ A1 A2 A3

R1 1 0 1

R2 1 1 0

R3 0 0 1

where A1,A2,A3 are values for NameA, R1,R2,R3 for NameR and 1,0,1,0.. value for Result field. Problem: Columns number can grow if I add new A objects

Question:

1)Is't there a HtmlHelper that display such a grid (from a list of objects RP)?

2)Is't there a HtmlHelper that display a grid from a DataTable object (I create a sql query that renders this grid)?

Thanks for help

+1  A: 

Anyway, even if you don't provide enough details, here it is. If you have IList<RP> you'll do

 <table>
  <tr>
      <th>R/A</th>
      <% foreach (var a in Model.Select(x => x.objA.NameA).OrderBy(x => x).Distinct()) { %>
           <th><%= a %></th>
      <% } %>
  </tr>
  <% foreach (var data in Model.GroupBy(x => x.objR.NameR)) { %>
      <tr> 
         <td><%= data.Key %></td>
         <% foreach (var aas in data.OrderBy(x => x.objA.NameA)) { %>
            <td><%= aas.result ? "1" : "0" %></td>
         <% } %>
      </tr>
  <% } %>
 </table>

Of course you better have this calculated in controller, so that you pass grouped model instead of a list to the view. And it assumes that you always have exact number of A's, or you'll have to extend logic a bit.

queen3