“由于对象的当前状态,操作无效。” 例外,当我要检索项目时

mz1378

我使用此方法在Blazor Serverside中检索Tree组件的项目,在DAL中,我有:

public List<TreeItem> GetTreeItems()
    {
        var tree = new List<TreeItem>();
        TreeItem item = new TreeItem()
        {
            DepartmentID = 0,
            CategoryID = 0,
            Text = "Root",
            Childs = context.Departments.OrderBy(d => d.Order).Select(d => new TreeItem()
            {
                DepartmentID = d.Id,
                CategoryID = 0,
                Text = d.Title,
                Childs = d.Categories.OrderBy(c => c.Order).Select(c => new TreeItem()
                {
                    DepartmentID = d.Id,
                    CategoryID = c.Id,
                    Text = c.Title                         
                }).ToList()
            }).ToList()
        };
        tree.Add(item);

        return tree;
    }

TreeItem类如下(blazor Component和Dal类共享的模型):

public class TreeItem
{
    public int DepartmentID { get; set; }
    public int CategoryID { get; set; }
    public string Text { get; set; }
    public List<TreeItem> Childs { get; set; }        
}

但是,当我在blazor组件中检索树的Items时,会得到一个例外:由于对象的当前状态,操作无效。,admin是我向Blazor组件注入的DAL类,如下所示:

 private void GetTreeModel()
{
    try
    {
        Items = admin.GetTreeItems();
        TreeSuccess = true;
        TreeMessage = "Success";
        return;
    }
    catch (Exception ex) // Error here
    {
        TreeSuccess = false;
        TreeMessage = "Can not load tree items";
        return;
    }
}

这是什么错误以及如何解决?

mz1378

我首先使用加载实体,然后对对象使用Linq来解决问题,如下所示:

var tree = new List<TreeItem>();
        var departments = context.Departments.OrderBy(d => d.Order).ToList();
        var categories = context.Categories.OrderBy(c => c.Order).ToList();
        TreeItem item = new TreeItem()
        {
            DepartmentID = 0,
            CategoryID = 0,
            Text = "Root",
            Childs = departments.Select(d => new TreeItem()
            {
                DepartmentID = d.Id,
                CategoryID = 0,
                Text = d.Title,
                Childs = categories.Where(c => c.DepartmentID == d.Id).OrderBy(c => c.Order).Select(c => new TreeItem()
                {
                    DepartmentID = d.Id,
                    CategoryID = c.Id,
                    Text = c.Title
                }).ToList()
            }).ToList()
        };



            tree.Add(item);

        return tree;
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

由于对象的当前状态,操作无效。当我删除按钮时

使用C#将记录插入Oracle数据库时出现错误-由于对象的当前状态,操作无效

尝试生成specflow步骤定义报告时收到错误消息“由于对象的当前状态,操作无效”

UPDATE操作-由于对象的当前状态,该操作无效

SqlBulkCopy 抛出“由于对象的当前状态,操作无效”

错误:“由于对象的当前状态,操作无效”

REST API:由于对象的当前状态,操作无效

由于对象的当前状态,IEquatable操作无效

为什么会收到此错误{“由于对象的当前状态,操作无效。”} C#

回发期间“由于对象的当前状态,操作无效”错误

由于Unity 3d中对象的当前状态,该操作无效

由于对象的当前状态(System.Text.Json),操作无效

由于C#实体框架中对象的当前状态,因此该操作无效

如何修复.Net Azure Function App中的“由于对象的当前状态,操作无效”错误?

当对象的当前状态不允许对其进行尝试操作时,将抛出哪个异常?

鉴于 ExecuteNonQuery() 中对象的当前状态,操作无效

由于对象的当前状态,该操作无效。使用dotnet core 5在eventhub中获取microsoft.azure.amqp

BatchErrorException:指定的操作对于资源的当前状态无效

当我单击“编辑”按钮时,要编辑其类别无效。错误:尝试获取非对象的属性“ id”

使用cURL检索TeamCity中的当前构建状态

当我要编辑时,对象引用未设置为对象的实例吗?

当我发布要切断的数据时,空对象引用

使用Bumblebee时,如何查询nvidia卡的当前状态?

Ember.js使用操作时要呈现页面的当前点击和新点击

从我的当前状态到当前状态的$ state.go是否会引发相关事件?

在Keras中,当我用N个单位创建有状态的LSTM层时,我到底要配置什么?

使用SQL检索Sparx EA中的当前选定对象

反应:componentDidUpdate - 当我更新数组中的对象时,前一个状态与当前状态相同

当我从列表中删除项目并执行setState((){})时,颜色状态正在改变