开发记录: 1.树保存到数据库2.数据库组装成树给前端

这篇博客详细记录了如何将前端传来的JSON数据保存到数据库,然后将数据库中的数据组装成树形结构,以便前端进行展示。主要涉及ASP.NET的后端处理和数据交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.前端传参json : 

{
    "testPaperName": "题型",
    "testPaperClassificationID": "4021672700657573888",
    "practicalScenario": "0",
    "testPaperTimeLimit": "0",
    "testPaperTotalScore": "9.00",
    "practicalObject": "",
    "testPaperCatalogue": [{
        "testPaperTemplateTopicVM": [],
        "testPaperTemplateTopicOption": [],
        "testPaperTemplateCatalogueID": "4024546987001290753",
        "testPaperTemplateID": "4024546987001290752",
        "catalogueName": "目录",
        "parentCatalogueID": "",
        "questionType": null,
        "sortNo": null,
        "nodeID": null,
        "children": [{
            "testPaperTemplateTopicVM": [],
            "testPaperTemplateTopicOption": [],
            "testPaperTemplateCatalogueID": "4024546987001290754",
            "testPaperTemplateID": "4024546987001290752",
            "catalogueName": "选择题",
            "parentCatalogueID": "4024546987001290753",
            "questionType": "1",
            "sortNo": null,
            "nodeID": null,
            "children": [{
                "testPaperTemplateTopicOption": [],
                "testPaperTemplateTopicID": "4024546987001290756",
                "testPaperTemplateCatalogueID": "4024546987001290754",
                "topicSortNo": "1",
                "topicSortName": "1",
                "optionCode": null,
                "score": 3,
                "dataType": "yes",
                "expanded": false,
                "_id": 3,
                "_uid": 3,
                "_pid": 2,
                "_level": 2,
                "type": "1",
                "catalogueName": "单选有正确答案",
                "isChange": 1,
                "_state": "modified",
                "topicID": "4024837615882903552",
                "topicDetail": {
                    "topicOptionVM": [{
                        "topicOptionID": "4024837615882903553",
                        "topicID": "4024837615882903552",
                        "optionCode": "A",
                        "optionContent": "选项1",
                        "optionImageFileAddress": null,
                        "score": null,
                        "optionDescribe": null,
                        "isCorrectAnswer": "n"
                    }, {
                        "topicOptionID": "4024837615882903554",
                        "topicID": "4024837615882903552",
                        "optionCode": "B",
                        "optionContent": "选项2",
                        "optionImageFileAddress": null,
                        "score": null,
                        "optionDescribe": null,
                        "isCorrectAnswer": "y"
                    }, {
                        "topicOptionID": "4024837615882903555",
                        "topicID": "4024837615882903552",
                        "optionCode": "C",
                        "optionContent": "选项3",
                        "optionImageFileAddress": null,
                        "score": null,
                        "optionDescribe": null,
                        "isCorrectAnswer": "n"
                    }, {
                        "topicOptionID": "4024837615882903556",
                        "topicID": "4024837615882903552",
                        "optionCode": "D",
                        "optionContent": "选项4",
                        "optionImageFileAddress": null,
                        "score": null,
                        "optionDescribe": null,
                        "isCorrectAnswer": "n"
                    }],
                    "fillBlankTopicVM": [],
                    "topicID": "4024837615882903552",
                    "topicType": "1",
                    "topicTypeName": "单选(有正确答案)",
                    "topicClassificationID": "4021672658894888960",
                    "topicClassificationName": "心理测评",
                    "topicTagIDs": "4021584123680563200",
                    "topicTagName": "CSSS",
                    "topicDryContent": "单选有正确答案",
                    "topicDryImgFileAddress": null,
                    "judgeRightKey": null,
                    "judgeRightKeyName": null,
                    "referenceAnswer": null,
                    "referenceAnswerImgFileAddress": null,
                    "userId": null,
                    "topicStatus": "1",
                    "topicStatusName": "启用"
                }
            }, {
                "testPaperTemplateTopicOption": [],
                "testPaperTemplateTopicID": "4024546987001290758",
                "testPaperTemplateCatalogueID": "4024546987001290754",
                "topicSortNo": "2",
                "topicSortName": "2",
                "optionCode": null,
                "score": 3,
                "dataType": "yes",
                "expanded": false,
                "_id": 4,
                "_uid": 4,
                "_pid": 2,
                "_level": 2,
                "type": "1",
                "catalogueName": "周周",
                "isChange": 1,
                "_state": "modified",
                "topicID": "4025996133872738304",
                "topicDetail": {
                    "topicOptionVM": [{
                        "topicOptionID": "4025996133872738305",
                        "topicID": "4025996133872738304",
                        "optionCode": "A",
                        "optionContent": "周周a改",
                        "optionImageFileAddress": "webFolder/2022/03/20/4025995934580383744__a.jpeg",
                        "score": null,
                        "optionDescribe": null,
                        "isCorrectAnswer": "n"
                    }, {
                        "topicOptionID": "4025996133872738306",
                        "topicID": "4025996133872738304",
                        "optionCode": "B",
                        "optionContent": "周周b改",
                        "optionImageFileAddress": "webFolder/2022/03/20/4025995953421197312__b.jpeg",
                        "score": null,
                        "optionDescribe": null,
                        "isCorrectAnswer": "n"
                    }, {
                        "topicOptionID": "4025996133872738307",
                        "topicID": "4025996133872738304",
                        "optionCode": "C",
                        "optionContent": "周周c改",
                        "optionImageFileAddress": null,
                        "score": null,
                        "optionDescribe": null,
                        "isCorrectAnswer": "n"
                    }, {
                        "topicOptionID": "4025996133872738308",
                        "topicID": "4025996133872738304",
                        "optionCode": "D",
                        "optionContent": "周周d改",
                        "optionImageFileAddress": null,
                        "score": null,
                        "optionDescribe": null,
                        "isCorrectAnswer": "n"
                    }, {
                        "topicOptionID": "4025996133872738309",
                        "topicID": "4025996133872738304",
                        "optionCode": "E",
                        "optionContent": "e",
                        "optionImageFileAddress": "webFolder/2022/03/20/4025996124490080256__4E4949AB-5B7F-4492-9036-A35054A863CB.jpeg",
                        "score": null,
                        "optionDescribe": null,
                        "isCorrectAnswer": "y"
                    }],
                    "fillBlankTopicVM": [],
                    "topicID": "4025996133872738304",
                    "topicType": "1",
                    "topicTypeName": "单选(有正确答案)",
                    "topicClassificationID": "",
                    "topicClassificationName": null,
                    "topicTagIDs": "4021584123680563200,4021584073449578496,4021583758239244288",
                    "topicTagName": "SAD,CS,CSSS",
                    "topicDryContent": "周周",
                    "topicDryImgFileAddress": null,
                    "judgeRightKey": null,
                    "judgeRightKeyName": null,
                    "referenceAnswer": null,
                    "referenceAnswerImgFileAddress": null,
                    "userId": null,
                    "topicStatus": "1",
                    "topicStatusName": "启用"
                }
            }, {
                "testPaperTemplateTopicOption": [],
                "testPaperTemplateTopicID": "4024546987001290760",
                "testPaperTemplateCatalogueID": "4024546987001290754",
                "topicSortNo": "3",
                "topicSortName": "3",
                "optionCode": null,
                "score": 3,
                "dataType": "yes",
                "expanded": false,
                "_id": 5,
                "_uid": 5,
                "_pid": 2,
                "_level": 2,
                "type": "1",
                "catalogueName": "变成单选",
                "isChange": 1,
                "_state": "modified",
                "topicID": "4026116202082639872",
                "topicDetail": {
                    "topicOptionVM": [{
                        "topicOptionID": "4026116202082639873",
                        "topicID": "4026116202082639872",
                        "optionCode": "A",
                        "optionContent": "1",
                        "optionImageFileAddress": null,
                        "score": null,
                        "optionDescribe": null,
                        "isCorrectAnswer": "n"
                    }, {
                        "topicOptionID": "4026116202082639874",
                        "topicID": "4026116202082639872",
                        "optionCode": "B",
                        "optionContent": "2",
                        "optionImageFileAddress": null,
                        "score": null,
                        "optionDescribe": null,
                        "isCorrectAnswer": "n"
                    }, {
                        "topicOptionID": "4026116202082639875",
                        "topicID": "4026116202082639872",
                        "optionCode": "C",
                        "optionContent": "3",
                        "optionImageFileAddress": null,
                        "score": null,
                        "optionDescribe": null,
                        "isCorrectAnswer": "n"
                    }, {
                        "topicOptionID": "4026116202082639876",
                        "topicID": "4026116202082639872",
                        "optionCode": "D",
                        "optionContent": "4",
                        "optionImageFileAddress": null,
                        "score": null,
                        "optionDescribe": null,
                        "isCorrectAnswer": "y"
                    }],
                    "fillBlankTopicVM": [],
                    "topicID": "4026116202082639872",
                    "topicType": "1",
                    "topicTypeName": "单选(有正确答案)",
                    "topicClassificationID": "4021672658894888960",
                    "topicClassificationName": "心理测评",
                    "topicTagIDs": "4021584123680563200,4021584073449578496,4021583758239244288,4021583151675777024",
                    "topicTagName": "asdasdzxc,SAD,CS,CSSS",
                    "topicDryContent": "变成单选",
                    "topicDryImgFileAddress": null,
                    "judgeRightKey": null,
                    "judgeRightKeyName": null,
                    "referenceAnswer": null,
                    "referenceAnswerImgFileAddress": null,
                    "userId": null,
                    "topicStatus": "1",
                    "topicStatusName": "启用"
                }
            }],
            "userID": null,
            "score": 9,
            "expanded": true,
            "_id": 2,
            "_uid": 2,
            "_pid": 1,
            "_level": 1
        }],
        "userID": null,
        "score": 9,
        "expanded": true,
        "_id": 1,
        "_uid": 1,
        "_pid": -1,
        "_level": 0
    }],
    "topicNum": 3,
    "userId": "3940096989560348672",
    "testPaperTemplateID": "4024546987001290752"
}

后端解析组装: 

 #region 组装数据:
foreach (var item in vm.TestPaperCatalogue)//第一级只有一层, “目录” 前端添加
            {
                JX_TestPaperCatalogueModel testPaperCatalogueModel = new JX_TestPaperCatalogueModel();
                testPaperCatalogueModel.TestPaperCatalogueID = SnowFlakeUse.GetSnowflakeID();
                testPaperCatalogueModel.TestPaperID = testPaperModel.TestPaperID;
                testPaperCatalogueModel.CatalogueName = item.CatalogueName;
                testPaperCatalogueModel.ParentCatalogueID = item.ParentCatalogueID;
                testPaperCatalogueModel.AddUser = vm.UserId;
                testPaperCatalogueList.Add(testPaperCatalogueModel);
                if (item.Children != null)
                {
                    resourceRootlog = RootCatalogDeal(item.Children, testPaperModel.TestPaperID, vm.UserId, ref testPaperCatalogueList, ref testPaperTopicList, ref testPaperTopicDetailList, testPaperCatalogueModel.TestPaperCatalogueID);

                }
            }
            #endregion

        #region 外部处理方法
        /// <summary>
        /// 递归处理子目录(赋值)
        /// </summary>
        /// <param name="ob"></param>
        /// <param name="ResourceRootCatalogID">根目录id</param>
        /// <returns></returns>
        public List<JX_TestPaperCatalogueModel> RootCatalogDeal(object ob, string TestPaperID, string UserId, ref List<JX_TestPaperCatalogueModel> catalogVM
            , ref List<JX_TestPaperTopicModel> testPaperTopicList, ref List<JX_TestPaperTopicDetailModel> testPaperTopicDetailList
            , string TestPaperCatalogueID = null, string CatalogID = null, string TopicCatalogueID = null, string QuestionType = null)
        {
            List<JX_TestPaperCatalogueVM> tChildren = JsonConvert.DeserializeObject<List<JX_TestPaperCatalogueVM>>(JsonConvert.SerializeObject(ob));
            if (tChildren.Count > 0)
            {
                int num = 0;
                int catalogNum = 1;
                // string TestPaperCatalogueID = string.Empty;
                foreach (var tc in tChildren)
                {
                    JX_TestPaperCatalogueModel catalog = new JX_TestPaperCatalogueModel();
                    // 如果有子节点,就在循环拿子节点,反之,叶子节点赋值
                    List<JX_TestPaperCatalogueVM> childrens = JsonConvert.DeserializeObject<List<JX_TestPaperCatalogueVM>>(JsonConvert.SerializeObject(tc.Children));

                        catalog.TestPaperCatalogueID = SnowFlakeUse.GetSnowflakeID();
                        if (tc.Children != null && childrens != null && childrens.Count > 0)
                        {
                            TopicCatalogueID = catalog.TestPaperCatalogueID;
                            QuestionType = tc.QuestionType;
                        }
                   


                    catalog.TestPaperID = TestPaperID;
                    if (string.IsNullOrWhiteSpace(CatalogID))
                    {
                        catalog.ParentCatalogueID = TestPaperCatalogueID;
                    }
                    else
                    {
                        catalog.ParentCatalogueID = CatalogID;
                    }
                    catalog.CatalogueName = tc.CatalogueName;
                    if (!string.IsNullOrWhiteSpace(tc.QuestionType))
                    {
                        catalog.QuestionType = tc.QuestionType;
                    }
                    catalog.AddUser = UserId;
                  
                    catalog.SortNo = catalogNum;
                    catalog.SortNoName = catalog.SortNo.ToString();
                    catalog.TopicID = tc.TopicID;
                    // 如果有子节点,就在循环拿子节点,反之,叶子节点赋值

                    if (tc.Children != null && childrens != null && childrens.Count > 0)
                    {
                        catalogNum++;
                        catalogVM.Add(catalog);
                        tc.Children = RootCatalogDeal(tc.Children, TestPaperID, UserId, ref catalogVM, ref testPaperTopicList, ref testPaperTopicDetailList, TestPaperCatalogueID, catalog.TestPaperCatalogueID, TopicCatalogueID, QuestionType);
                    }
                    else
                    {
                        num++;
                        //叶子节点
                        JX_TestPaperTopicModel testPaperTopicModel = new JX_TestPaperTopicModel();
                        testPaperTopicModel.TestPaperTopicID = SnowFlakeUse.GetSnowflakeID();
                        testPaperTopicModel.TestPaperCatalogueID = TopicCatalogueID;
                        testPaperTopicModel.TopicSortNo = num.ToString();
                        testPaperTopicModel.TopicSortNoName = testPaperTopicModel.TopicSortNo.ToString();
                        testPaperTopicModel.TopicID = tc.TopicID;
                        testPaperTopicModel.QuestionType = QuestionType;
                        if (!string.IsNullOrWhiteSpace(tc.Score))
                        {
                            testPaperTopicModel.TopicScore = tc.Score;
                        }
                        if (!string.IsNullOrWhiteSpace(tc.ScoringRule))
                        {
                            testPaperTopicModel.ScoringRule = tc.ScoringRule;
                        }

                        testPaperTopicModel.AddUser = UserId;
                        testPaperTopicModel.AddTime = DateTime.Now.ToString();
                        testPaperTopicList.Add(testPaperTopicModel);
                        //叶子节点 是否有单选(没有正确答案)
                        foreach (var item in tc.TestPaperTopicDetail)
                        {
                            JX_TestPaperTopicDetailModel testPaperTopicDetailModel = new JX_TestPaperTopicDetailModel();
                            testPaperTopicDetailModel.TestPaperTopicDetailID = SnowFlakeUse.GetSnowflakeID();
                            testPaperTopicDetailModel.TestPaperTopicID = testPaperTopicModel.TestPaperTopicID;
                            testPaperTopicDetailModel.Option = item.Option;
                            if (!string.IsNullOrWhiteSpace(item.Score))
                            {
                                testPaperTopicDetailModel.Score = Convert.ToDecimal(item.Score);
                            }
                            testPaperTopicDetailModel.AddUser = UserId;
                            testPaperTopicDetailModel.AddTime = DateTime.Now;
                            testPaperTopicDetailList.Add(testPaperTopicDetailModel);
                        }

                    }

                }
            }
            return catalogVM;
        }
 #endregion

2.数据库组装成树

var rootCatalog = DataConvert<JX_TestPaperCatalogueVM>.ConvertToEntityList(table);//得到目录
// 资源目录list
                            var catalog = rootCatalog;
                            List<JX_TestPaperCatalogueVM> first = new List<JX_TestPaperCatalogueVM>();
                            if (catalog.Count > 0)
                            {
                                // 第一个目录节点
                                var firstRoot = catalog.Where(p => string.IsNullOrEmpty(p.ParentCatalogueID)).ToList();
                                foreach (var item in firstRoot)
                                {
                                    List<JX_TestPaperCatalogueVM> root = new List<JX_TestPaperCatalogueVM>();
                                    var rootlog = catalog.Where(p => p.ParentCatalogueID == item.TestPaperCatalogueID).ToList();
                                    // 递归处理子节点
                                    root = RootCatalogTreeDeal(item.TestPaperCatalogueID, catalog, testPaperTopic, testPaperTopicDetail, topicDetaillist);

                                    first.Add(new JX_TestPaperCatalogueVM()
                                    {
                                        TestPaperCatalogueID = item.TestPaperCatalogueID,
                                        TestPaperID = item.TestPaperID,
                                        CatalogueName = item.CatalogueName,
                                        ParentCatalogueID = string.Empty,
                                        Children = root,

                                    });
                                }
                            }



        #region 递归处理子目录树状
        /// <summary>
        ///  递归处理子目录树状(赋值)
        /// </summary>
        /// <param name="parentId"></param>
        /// <param name="catalog"></param>
        /// <returns></returns>
        public List<JX_TestPaperCatalogueVM> RootCatalogTreeDeal(string parentId, List<JX_TestPaperCatalogueVM> catalog = null
            , List<JX_TestPaperTopicVM> testPaperTopic = null, List<JX_TestPaperTopicDetailVM> testPaperTopicDetail = null,List<JX_TopicDetailVM> topicDetaillist=null)
        {
            List<JX_TestPaperCatalogueVM> catalogVM = new List<JX_TestPaperCatalogueVM>();
            var tChildren = catalog.Where(t => t.ParentCatalogueID == parentId).ToList();
            tChildren = tChildren.OrderBy(t => t.SortNo).ToList();
            if (tChildren.Count > 0)
            {
                foreach (var tc in tChildren)
                {
                    JX_TestPaperCatalogueVM rootTreeVM = new JX_TestPaperCatalogueVM();
                    rootTreeVM.CatalogueName = tc.CatalogueName;
                    rootTreeVM.ParentCatalogueID = tc.ParentCatalogueID;
                    rootTreeVM.TestPaperID = tc.TestPaperID;
                    rootTreeVM.TestPaperCatalogueID = tc.TestPaperCatalogueID;
                    rootTreeVM.QuestionType = tc.QuestionType;
                    rootTreeVM.SortNo = tc.SortNo;
      // rootTreeVM.Score += GetChildren(catalog, tc.TestPaperTemplateCatalogueID).ToString();//求子节点分数
                    if (!string.IsNullOrWhiteSpace(tc.QuestionType))
                    {
                       
                        List<JX_TestPaperTopicVM> topic = testPaperTopic?.Where(t => t.TestPaperCatalogueID == rootTreeVM.TestPaperCatalogueID).ToList();
                        if (topic != null)
                        {
                            foreach (var temp in topic)
                            {
                                temp.DataType = "yes";
                                temp.TestPaperTopicDetail = testPaperTopicDetail.Where(t => t.TestPaperTopicID == temp.TestPaperTopicID).ToList();
                                if(!string.IsNullOrWhiteSpace(temp.TopicID))//题目详情
                                {
                                    temp.TopicDetail= topicDetaillist.Where(x => x.TopicID == temp.TopicID).FirstOrDefault();
                                }
                              
                            }
                            rootTreeVM.Children = topic;
                        }
 // rootTreeVM.Score = tc.Score.ToString();
                        catalogVM.Add(rootTreeVM);
                    }
                    else
                    {
                        rootTreeVM.Children = (RootCatalogTreeDeal(tc.TestPaperCatalogueID, catalog, testPaperTopic, testPaperTopicDetail, topicDetaillist));
                        catalogVM.Add(rootTreeVM);
                    }
                }
            }
            return catalogVM;
        }
        #endregion


 #region 获取子节点的分数
        /// <summary>
        /// 获取子节点的分数
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="ID"></param>
        /// <returns></returns>
        private decimal GetChildren(IEnumerable<JX_TestPaperTemplateCatalogueVM> dt, string ID)
        {
            decimal Score = 0;
            List<JX_TestPaperTemplateCatalogueVM> nodeList = new List<JX_TestPaperTemplateCatalogueVM>();
            var children = dt.Where(q => q.ParentCatalogueID == ID);
            foreach (var dr in children)
            {
                Score +=Convert.ToDecimal(dr.Score);
               GetChildren(dt, dr.TestPaperTemplateCatalogueID);
            }
            return Score;
        }
        #endregion

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值