views:

1355

answers:

1

Hi,

Does anyone have any suggestions/ best practices for doing master/ detail pages using the ASP.NET MVC Framework?

Background

I've got a page of Client information, under each one I need to display 0 or more related records about that client.

The database schema looks something like this:

Clients -- one to many --> ClientData

I'm currently using LINQ for the data classes which is being populated using stored procedures. So to get the client information I'd write:

var clientQuery = from client in dataContext.GetClients ()
orderby client.CompanyName
select client;

When each client is rendered in the view, I need to display the related data, so I'd need to call this for each client found:

var clientDataQuery = from clientData in dataContext.GetClientData (client.ClientId)
orderby clientData.CreatedDate
select clientData;

Question

What's the best of achieving this?

  1. Use the ViewData and store each query
  2. Use the typed model of the view (perhaps using a class to represent a client with the related information in a list
  3. Other...

Thanks, Kieron

+2  A: 

Use a IDictionary>, which you should then wrap into a viewdata class.

This will allow you to do the filtering/sorting at the controller level while maintaining a strict correlation between the client and its data.

Example:

public class ClientController{
    public ViewResult ClientList(){
     var clientsAndData = new Dictionary<Client,IEnumerable<ClientData>>();

     var clients = from client in dataContext.GetClients()
      orderby client.CompanyName
      select client;

     foreach( var client in clients )
       clientsAndData.Add(
   client,
   (
    from clientData in dataContext.GetClientData(client.ClientId)
    orderby clientData.CreatedDate
    select clientData
   ).ToList()
    );

     return View( new ClientListViewData{ Clients = clientsAndData } );
    }
}

public class ClientListViewData{
    public IDictionary<Client,IEnumerable<ClientData>> Clients{ get; set; }
}
Troy