I'm using nested JSON data from my GeoRegionCountries APIController & custom class TreeView is used to format the data as per the required nested structure of plugin I'm using.
(我正在使用来自GeoRegionCountries APIController的嵌套JSON数据,并且自定义类TreeView用于根据所需的插件嵌套结构来格式化数据。)
I am using a combo multi select Treeview using this jquery plugin Multi-Select Drop Down Tree Plugin you can see it by this link enter link description here(我正在使用使用此jquery插件的组合多选Treeview多选下拉树插件,您可以通过此链接查看它在此处输入链接描述)
My Entity class is GeoSalesTerrRegCountry as below
(我的实体类是GeoSalesTerrRegCountry ,如下所示)
namespace ReportingSystemDataModels.EntityModel
{
using System;
using System.Collections.Generic;
public partial class GeoSalesTerrRegCountry
{
public int GeoKey { get; set; }
public string GeoName { get; set; }
public Nullable<int> ParentId { get; set; }
public Nullable<int> SalesTerritoryKey { get; set; }
public virtual DimSalesTerritory DimSalesTerritory { get; set; }
}
}
My APIController is below
(我的APIController在下面)
[Route("GetTerritories")]
[HttpGet]
[EnableCors("*", "*", "GET")]
public HttpResponseMessage GetTerritories()
{
// IEnumerable<TreeView> geo = new IEnumerable<TreeView>();
IEnumerable<GeoSalesTerrRegCountry> territories = _salesServices.GetAllRegCountries();
if (territories != null)
{
if (territories.Any())
{
var geoTrees = (IEnumerable<TreeView>)GeoExtensions.BuildTrees(territories.ToList());
return Request.CreateResponse(HttpStatusCode.OK, geoTrees);
}
}
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Geography not found");
}
The TreeView class(to select only the field as per the 3rd party jquery plugin) and extension methods used are to build tree & format it as per the 3rd party jquery plugin
(TreeView类(仅根据第3方jquery插件选择字段)和扩展方法是根据第3方jquery插件构建树并将其格式化)
using ReportingSystemDataModels.EntityModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ReportingSystem.Areas.gijgoDefaultFormatAPI
{
public class TreeView
{
public int? Id { get; set; }
public string Title { get; set; }
public int? ParentId { get; set; }
public virtual IEnumerable<TreeView> Subs { get; set; }
}
//public class
public static class GeoExtensions
{
public static IList<TreeView> BuildTrees(this IList<GeoSalesTerrRegCountry> geos)
{
var dtos = geos.Select(c => new TreeView
{
Id = c.GeoKey,
Title = c.GeoName,
ParentId = c.ParentId,
}).ToList();
dtos = dtos.GroupBy(x => x.Title).Select(g => g.First()).ToList();
return BuildTrees(null, dtos);
}
private static IList<TreeView> BuildTrees(int? pid, IList<TreeView> candicates)
{
var subs = candicates.Where(c => c.ParentId == pid);
if (subs.Count() == 0)
{
return new List<TreeView>(); // required an empty list instead of a null !
}
foreach (var i in subs)
{
i.Subs = BuildTrees(i.Id, candicates);
}
if (subs.Count() > 0)
{
foreach (var i in subs)
{
i.Subs = BuildTrees(i.Id, candicates);
}
}
return subs.ToList(); ;
}
}
}
I've used this solution in this link(same problem) but there's a difference in JSON data returned between mine and theirs.
(我在此链接中使用了此解决方案(相同的问题),但我的与其返回的JSON数据有所不同。)
enter link description here
(在此处输入链接说明)
In this link, JSON data is returned in this format(with no empty property array)
(在此链接中,JSON数据以这种格式返回(没有空属性数组))
[
{
id: 0,
title: 'Item 1 '
},
{
id: 1,
title: 'Item 2',
subs: [
{
id: 10,
title: 'Item 2-1'
},
]
},
]
My JSON returns like this
(我的JSON这样返回)
[
{
"Id": 1,
"Title": "United States",
"ParentId": null,
"Subs": [
{
"Id": 7,
"Title": "Northwest",
"ParentId": 1,
"Subs": []
},
{
"Id": 8,
"Title": "Northeast",
"ParentId": 1,
"Subs": []
},
{
"Id": 9,
"Title": "Central",
"ParentId": 1,
"Subs": []
},
{
"Id": 10,
"Title": "Southwest",
"ParentId": 1,
"Subs": []
},
{
"Id": 18,
"Title": "Southeast",
"ParentId": 1,
"Subs": []
}
]
},
{
"Id": 2,
"Title": "Canada",
"ParentId": null,
"Subs": []
},
{
"Id": 3,
"Title": "France",
"ParentId": null,
"Subs": []
},
{
"Id": 4,
"Title": "Germany",
"ParentId": null,
"Subs": []
},
{
"Id": 5,
"Title": "Australia",
"ParentId": null,
"Subs": []
},
{
"Id": 6,
"Title": "United Kingdom",
"ParentId": null,
"Subs": []
}
];
So, there are no error in the scripts or any plugin related error in page but the data found to the control is "undefined".
(因此,脚本中没有错误,或者页面中没有任何插件相关的错误,但是控件中找到的数据是“未定义的”。)
My JSON response is unable to bind to the third party plugin.(我的JSON响应无法绑定到第三方插件。)
I'm fetching this data and using the first 3 fields
(我正在获取此数据并使用前3个字段)
GeoKey GeoName ParentId SalesTerritoryKey 1 United States NULL NULL 2 Canada NULL 6 3 France NULL 7 4 Germany NULL 8 5 Australia NULL 9 6 United Kingdom NULL 10 7 Northwest 1 1 8 Northeast 1 2 9 Central 1 3 10 Southwest 1 4 18 Southeast 1 5
(GeoKey GeoName ParentId SalesTerritoryKey 1美国NULL NULL 2加拿大NULL 6 3法国NULL 7 4德国NULL 8 5澳大利亚NULL 9 6英国NULL 10 7西北1 1 8东北1 2 9中央1 3 10西南1 4 18东南1 5)
My ajax call is
(我的ajax电话是)
$(document).ready(function () {
alert("hello");
$.ajax({
url: "http://localhost:51604/api/Sales/GetTerritories",
type: "Get",
data: "",
dataType: "json",
cache: false,
success: function (data) {
$('#GeoComboTree').comboTree({
source: data,
isMultiple: false
});
},
error: function (data) {
}
});
cshtml page
(cshtml页面)
@section head {
<head>
<title>Manage Ajax Sourced Data With Grid</title>
<meta charset="utf-8" />
https://fonts.googleapis.com/icon?family=Material+Icons
<script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ"
crossorigin="anonymous"></script>
<script src="~/Scripts/comboTreePlugin/icontains.js"></script>
<script src="~/Scripts/comboTreePlugin/comboTreePlugin.js"></script>
</head>
}
<div class="col-3">
<div class="form-group">
<label class="form-control-label"></label>
<input type="text" id="GeoComboTree" placeholder="Select" />
</div>
</div>
It doesn't work - no script error nor server side error but undefined is bound to the Multi-Select Drop Down Tree Plugin enter link description here
(它不起作用-没有脚本错误也没有服务器端错误,但未定义绑定到Multi-Select Drop Down Tree Plugin,请在此处输入链接描述)
ask by devvab translate from so