【问题标题】:How to write the Looping structure for Parent-Child relationships?如何编写父子关系的循环结构?
【发布时间】:2015-07-08 06:50:15
【问题描述】:

我正在使用 treetable.js。作为程序,我保存了子 ID 和父 ID。 insert 数据插入成功。但返回数据可能仅显示在表格中或出现分组错误。 然后我使用 IGrouping 对象得到转换错误。 请告诉我下面的子父循环结构。

在模型中:

public string name {get;set;}    
public int childId {get;set;}
public int  ParentId {get;set;}

在控制器中:

var list = db.table.groupby(s=>s.parentId).toList();
   return view(list);

鉴于:

 <table id="example-basic">
         <tr>

             <th>Name</th>
             <th>Data Id</th>
             <th>Parent Id</th>
         </tr>
     @foreach (var item in Model)
     {
        <tr data-tt-id='@item.dataid' data-tt-parent-id='@item.dataparentid'>

            <td>@item.Name</td>
            <td>@item.dataid</td>
            <td>@item.dataparentid</td>
        </tr>
     }
 </table>

【问题讨论】:

  • 什么是“转换错误”?还是“分组错误”?如果您列出您收到的错误,则更容易为您的问题提供准确的答案。
  • ChildId 是你的行 Id,我猜对了吗?
  • 传入字典的模型项的类型为“System.Collections.Generic.List1[System.Linq.IGrouping2[System.Nullable1[System.Int32],Project_Cost_Management_System.Models.ChartOfAccount]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[Project_Cost_Management_System.Models.ChartOfAccount]”。
  • 不,ID 在 GUID @teovankot

标签: asp.net-mvc razor asp.net-mvc-5 parent-child


【解决方案1】:

如果你想摆脱分组异常,你应该这样写:

  var list = db.table.groupby(s => s.parentId).Select(x => new ChartOfAccount 
  { 
       ParentId = x.Key,
       childId = x.First().childId,
       name = x.First().name
  }).toList();
  return view(list);

如您所见,每个 ParentId 因为或分组,您只会得到一行。

但我猜你在寻找别的东西。

【讨论】:

  • return view(list); 上获取错误为The entity or complex type 'Project_Cost_Management_System.Models.ChartOfAccount' cannot be constructed in a LINQ to Entities query.
  • @SaravananArunagiri 我想这是因为您的实体具有更多属性。你不想创建ViewModelChartOfAccount 类并在Controller 中获取它并在View 上使用吗?
猜你喜欢
  • 2014-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-07
  • 1970-01-01
  • 1970-01-01
  • 2010-12-25
  • 1970-01-01
相关资源
最近更新 更多