public JsonResult GetRoot(string id) { var items = new List <NewJSTreeVM>(); NewJSTreeVM rootModel = new NewJSTreeVM(); string parentId = string.Empty; bool isChildren = false; if (id.Contains("##")) { parentId = id; isChildren = true; id = id.Split("##")[1]; } DataTable dt = _partService.GetTreeViewData(id); if (dt != null && dt.Rows.Count > 0) { if (!isChildren) { rootModel = new NewJSTreeVM() { id = dt.Rows[0]["parentPartId"]?.ToString(), text = dt.Rows[0]["parentName"]?.ToString(), children = new List <JsTreeViewModel>() }; } else { rootModel = new NewJSTreeVM() { id = parentId, text = dt.Rows[0]["parentName"]?.ToString(), children = new List <JsTreeViewModel>() }; } foreach (var rows in dt.Select("LEVEL = '0'")) { string text = string.Empty; if (dt.Select("parentPartId = '" + rows["childPartId"]?.ToString() + "'").Any()) { text = dt.Select("parentPartId = '" + rows["childPartId"]?.ToString() + "'").FirstOrDefault()["parentName"]?.ToString(); } else { text = _partService.GetPartById(Convert.ToInt32(rows["childPartId"]?.ToString()))?.PartName; } rootModel.children.Add(new JsTreeViewModel() { id = dt.Rows[0]["parentPartId"]?.ToString() + "##" + rows["childPartId"]?.ToString() + "##", children = dt.Select("parentPartId = '" + rows["childPartId"]?.ToString() + "'").Any() ? true : false, text = text }); } } else { rootModel = new NewJSTreeVM() { id = id, text = "No Part", children = null }; } items.Add(rootModel); return(Json(items)); }