Reputation: 27107
Does anyone have any suggestions/ best practices for doing master/ detail pages using the ASP.NET MVC Framework?
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;
What's the best of achieving this?
Thanks, Kieron
Upvotes: 2
Views: 2600
Reputation: 1649
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; }
}
Upvotes: 2