Return object as Json

phpbutcher

New member
Joined
Mar 24, 2022
Messages
2
Programming Experience
5-10
Hi all,

I need to start by saying I am in no way a .Net/C# developer. I do however have to make changes to applications that were originally done by a group of contractors that are no longer with the company. The app currently has code that writes Json to the HTML as a ViewBag. I'm trying to make it into a method that will just return the data as Json for an Ajax call instead of writing it to the HTML source. I've tried a few different things I saw googling but the closest I came was getting a string with the data and all the double quotes escaped. Not an actual Json object. Here's the code I'm trying to make work but I don't seem to be able to make the jsTreeModel return as Json.

Any help is appreciated.

C#:
      public JsonResult PopulateJsTree(string subjectType)
        {
            RclSubjectsData dataObj = new RclSubjectsData();
            List<KeyValue> objRclSubjects = null;
            string jsonModel = "";
 
            List<KeyValue> tempRCLSubject = new List<KeyValue>();
            List<KeyValue> tempCRSubject = new List<KeyValue>();
 
            try
            {
                tempRCLSubject = dataObj.GetRCLSubjectData();
                tempCRSubject = dataObj.GetRCLCRSubjectData();
                if (subjectType == "RCL")
                {
                    objRclSubjects = dataObj.GetRCLSubjectData();
                }
                else if (subjectType == "CR")
                {
                    objRclSubjects = dataObj.GetRCLCRSubjectData();
                }
                else if (subjectType == "LC")
                    objRclSubjects = Util.GetLCClassSubjectListData();
 
                if (objRclSubjects != null)
                {
                    objRclSubjects = objRclSubjects.OrderBy(lst => lst.Key).ToList();
                    JsTreeModel[] jsTreeModel = new JsTreeModel[objRclSubjects.Count];
                    int count = 0;
                    foreach (KeyValue objKeyValue in objRclSubjects)
                    {
                        JsTreeModel objNode = new JsTreeModel();
 
                        // Attributes
                        JsTreeAttribute attr = new JsTreeAttribute();
 
                        objNode.id = objKeyValue.Value;
                        objNode.text = objKeyValue.Name;
                        objNode.parent = "#";
                        attr.subjectText = objKeyValue.Name;
                        attr.subjectLevel = 0;
                        attr.searchString = objKeyValue.Name;
                        attr.id = objKeyValue.Value;
                        attr.selected = false;
                        attr.isAssigned = false;
                        objNode.li_attr = attr;
                        if (HasChildNodes(objKeyValue.Value, subjectType, 0, tempRCLSubject, tempCRSubject)) {
                            objNode.li_attr.isParent = true;
                            objNode.children = true;
                        } else {
                            objNode.li_attr.isParent = false;
                            //objNode.children = [];
                        }
                        jsTreeModel[count] = objNode;
                        count++;
                    }
 
                    //jsonModel = new JavaScriptSerializer().Serialize(jsTreeModel);
                    JsonResult jsonResp = new Json(jsTreeModel);
                    //ViewBag.jsonModel = jsonModel;
                }
            }
            catch (Exception ex)
            {
                LogUtils.LogException(ex);
            }
          
            return jsonModel;
        }
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,712
Location
Chesapeake, VA
Programming Experience
10+
Just change line 59 to:
C#:
return new Json(jsTreeModel);
 
Top Bottom