简体   繁体   English

如何将父子表传递给我的视图

[英]How pass a parent child table to my view

I have a parent child table of Evolution progress in my db 我的数据库中有一个Evolution进展的父子表

 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: 我需要显示在我的asp.net-mvc页面中,如下所示:

 -------------------------------------
 |  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 我现在能做的是先读取parentID = null的Evolutions

 A0
 B0

and then create a list for each one using a function to bring the childs from db: 然后使用函数从db提取子对象,为每个列表创建一个列表:

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

Finally pass the List in a ViewBag 最后在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. 我的显示没有问题,我可以创建一个Evolution模板以在每个表单元格中呈现对象。

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: 假设您使用的是Entity Framework,并且具有Parent关系,则可能会发生以下情况:

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

Then you would build your data structure in memory. 然后,您将在内存中构建数据结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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