1

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)?

1
  • First of all your code is wrong, where're the collections? Commented Nov 19, 2009 at 16:25

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.