简体   繁体   中英

How pass a parent child table to my view

I have a parent child table of Evolution progress in my db

 EvolutionID   ParentID  otherFields
 A0              NULL      *
 A1              A0        *
 A2              A1        *
 B0              NULL      *
 B1              B0        *
 B2              B1        *

I need to display in my asp.net-mvc page like this:

 -------------------------------------
 |  A0       |  A1       |  A2       |
 |  field.*  |  fields.* |  fields.* | 
 -------------------------------------
 |  B0       |  B1       |  B2       |
 |  field.*  |  fields.* |  fields.* | 
 -------------------------------------

What I can do now is first read the Evolutions with parentID = null

 A0
 B0

and then create a list for each one using a function to bring the childs from db:

  List<Evolution> A0 = GetEvolutionListFor ('A0');
  List<Evolution> B0 = GetEvolutionListFor ('B0');

Finally pass the List in a ViewBag

  List<List<Evolution>> EvolutionList = New List<List<Evolution>>() {
        A0, A1 };
  ViewBag.Evolutions = EvolutionList ;

I dont have problem with the display, I can create a Evolution template to render the object in each table cell.

My problem is what is the best way to bring the data to the model.

Right now is one query to bring the root nodes and one more query for each node to bring the list.

Should I retrive all the rows at once and build the lists on my client app. Or is there a way I can retrive that data direct from database?

Assuming you can keep all of the records in memory at the same time, you should do one query of all the objects and then build the data structure in memory. Assuming you are using Entity Framework and have a Parent relation it would be something along the lines of:

List<Evolutions> evolutions; 
using (var context = new MyContext()) {
    evolutions = context.Evolutions.Include( e => e.Parent);
}

Then you would build your data structure in memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM