c# - 从 Razor Pages 中的数据库模型填充 TreeView

标签 c# razor treeview razor-pages kendo-treeview

我有一个 Razor 页面项目,它试图从使用数据模型创建的数据库填充 Kendo TreeView(或任何其他 TreeView)。

这些是模型的相关字段:

namespace FliveRetry.Models
{
    public class Org
    {
        public int ID { get; set; }
        public string OrgName { get; set; }
        public int? ParentID { get; set; }
        public bool? HasChildren { get; set; }
    }
}

这是 View 调用:
@(Html.Kendo().DropDownTree()
    .Name("selectedOrgs")
    .DataTextField("Org")
    .DataValueField("OrgID")
    .Checkboxes(true)
    .DataSource(d => d
        .Read(read =>
            read.Url("/Apps/Edit?handler=Read")
        )
    )
)

在 OnGetRead 中将值硬编码到如下列表中工作正常:
public IActionResult OnGetRead(int? id)
    {       
        var data = new List<Org>()
        {
            new Org() { ID = 1, ParentID = null, HasChildren = true, OrgName = "Org1" },
            new Org() { ID = 2, ParentID = null, HasChildren = false, OrgName = "Org2" },
            new Org() { ID = 3, ParentID = 1, HasChildren = true, OrgName = "Org3" },
            new Org() { ID = 4, ParentID = 3, HasChildren = false, OrgName = "Org4" }
        };

        var result = data.Where(x => id.HasValue ? x.ParentID == id : x.ParentID == null)
            .Select(item => new {
                id = item.ID,
                Org = item.OrgName,
                hasChildren = item.HasChildren
            });
            return new JsonResult(result);
        }
    }

Hard Coded Values Image

我正在尝试遍历 Org 集合,以使用数据库中的 Orgs 填充 Treeview 。

将 for 循环放在 List 声明中会在任何地方产生错误,OnGetRead 本身表示并非所有代码路径都返回一个值:
public IActionResult OnGetRead(int? id)
        {
            var blnChildren = false;
            var data = new List<Org>()
            {       
                foreach (var item in _context.Org)
                {
                    blnChildren = false;
                    if (item.ParentID.Equals(null))
                        {
                        blnChildren = true;
                        }
                    new Org() { ID = item.ID, ParentID = item.ParentID, HasChildren = blnChildren, OrgName = item.OrgName };
                }       
            };

            var result = data.Where(x => id.HasValue ? x.ParentID == id : x.ParentID == null)
                .Select(item => new {
                    id = item.ID,

                    Org = item.OrgName,
                    hasChildren = item.HasChildren
                });

            return new JsonResult(result);
        }

如果我将 for 循环放在列表声明之外,循环会正确填充但结果为空,我猜是因为项目声明在列表之外?
public IActionResult OnGetRead(int? id)
        {
            var blnChildren = false;
            var data = new List<Org>()
            {       

            };
            foreach (var item in _context.Org)
            {
                blnChildren = false;
                if (item.ParentID.Equals(null))
                    {
                    blnChildren = true;
                    }
                new Org() { ID = item.ID, ParentID = item.ParentID, HasChildren = blnChildren, OrgName = item.OrgName };
            }

            var result = data.Where(x => id.HasValue ? x.ParentID == id : x.ParentID == null)
                .Select(item => new {
                    id = item.ID,
                    Org = item.OrgName,
                    hasChildren = item.HasChildren
                });

            return new JsonResult(result);
        }

不确定我的问题是与 C# 相关还是与模型相关,但任何帮助将不胜感激。

如果我在错误的轨道上,从 Razor 项目而不是 MVC 项目中的数据库模型工作的自引用 Treeview 的任何其他工作示例也将有所帮助。

我有另一个使用 Kendo.Mvc.UI.TreeViewItemModel 数据的相同问题的例子,我也无法在硬编码之外工作,我只需要任何真正有效的版本。

谢谢堆

最佳答案

很多东西你需要纠正。

  • 在 forach 循环中,您每次都遍历数据库中的每个 Org 表项,创建对数据库的查询。那是浪费时间和资源。您应该首先从数据库中查询您需要的所有项目,然后对其进行处理。如果您需要所有表格条目,您可以将它们具体化。
  •     var orgs = _context.Org.ToArray();
        foreach(var item in orgs)
        {
           //code here
    
  • 您没有在 foreach 循环内将项目添加到数据列表中。你可以这样做
  •     var tmpOrg = new Org() { code code code} ;
        data.Add(tmpOrg);
    
  • 这整件事可以简化为一个表达式。 您不需要具体化整个表,也不需要那个 foreach 循环。您可以更改结果表达式以使用 _context.Org 而不是数据。在 hasChildren 的 Select 子句中,您可以更改这一行
  •     hasChildren = item.ParentID == null ? true : false;
    

    编辑

    你应该留下的是
    public IActionResult OnGetRead(int? id)
    {       
        var result = _context.Org.Where(x => id.HasValue ? x.ParentID == id : x.ParentID == null)
                                 .Select(item => new 
                                  {
                                      OrgID = item.ID,
                                      Org = item.OrgName,
                                      HasChildren = item.ParentID == null ? true : false
                                  });
    
        return new JsonResult(result);       
    }
    
    

    关于c# - 从 Razor Pages 中的数据库模型填充 TreeView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57950680/

    相关文章:

    c# - 如何将 Excel 文件读入矩阵?

    asp.net-mvc-4 - 使用 Razor 创建数字上下或值控制

    c# - 使用 Json.NET 替代 int、float 和 decimal

    c# - 多线程检查

    asp.net-mvc - ASP.NET MVC : Generating multi level menu using recursive helpers

    html - Bootstrap 模态框始终位于页面顶部

    c# - 在 WPF Treeview 中搜索特定的 TreeViewItem

    c++ - Qt 中可以使用哪个函数来选择 TreeView 中的项目?

    c# - 一棵树应该只有一个根节点吗

    c# - 如何使用 google Hangouts chat api webhooks 在同一线程上发布消息?