Estoy enviando datos de la base de datos a la página con Ajax. Quiero mostrar estos datos con jsTree. Hay tantas carpetas como datos envío. pero no muestra sus nombres. ¿Cómo puedo mostrar correctamente los datos con jsTree?


Mi código JavaScript:
$(function () { $.ajax({ type: "GET", url: "/Home/anaKategori", success: function (data) { console.log(data); createJSTrees(data); }, error: function (xhr, ajaxOptions, thrownError) { alert(xhr.status); alert(thrownError); } }); }); function createJSTrees(json) { $('#jstree').jstree({ 'core': { 'data': json }, "plugins": ["themes", "data", "ui"] }); }Mi código C# (.net core 5.0):
public List<tumKategoriler> anaKategori() //public string anaKategori() //public IActionResult anaKategori() { foreach (var item in context.urunKategorileri.ToList()) { if (context.urunKategorileri.Any(x=>x.urunKategorileriUstId == item.urunKategorileriId)) { List<altKategoriler> p = new List<altKategoriler>(); foreach (var altItem in context.urunKategorileri.Where(x => x.urunKategorileriUstId == item.urunKategorileriId)) { altKategoriler e = new altKategoriler { ad = altItem.urunKategorileriKategoriAdi }; p.Add(e); } tumKategoriler i = new tumKategoriler { ustKategoriAdi = item.urunKategorileriKategoriAdi, AltKategoriAdlari = p }; json.Add(i); } else { tumKategoriler o = new tumKategoriler { ustKategoriAdi = item.urunKategorileriKategoriAdi }; json.Add(o); } } return json; //return JsonConvert.SerializeObject(json); //return Json(new { jsonvar = JsonConvert.SerializeObject(json) }); }Su estructura de datos puede ser incorrecta. Puede intentar devolver List<JsTreeModel> en anaKategori :
public class JsTreeModel { public string Id { get; set; } public string Text { get; set; } public string Parent { get; set; } = "#"; }Si no tiene un padre, Padre = "#". Si tiene un padre, establezca la identificación del padre. a la propiedad matriz.
Aquí hay datos de muestra:
[{ id: "padre1", parent: "#", text: "Padre 1", }, { id: "padre2", parent: "#", text: "Padre 2" }, { id: "id3", parent: "padre1", text: "Figlio 1 di padre 1" }, { id: "id4", parent: "padre1", text: "Figlio 2 di padre 1" }, { id: "id5", parent: "padre2", text: "Figlio 1 di padre 2" }, { id: "id6", parent: "id5", text: "Figlio 1 di figlio 1 di padre 2" }, { id: "id7", parent: "id5", text: "Figlio 2 di figlio 1 di padre 2" }, { id: "id8", parent: "id5", text: "Figlio 3 di figlio 1 di padre 2" }, { id: "id9", parent: "#", text: "Figlio 3 di figlio 1 di padre 2" }]resultado:
I la url del código ajax te perdiste el tipo de archivo. ******************************** tu ajax
$.ajax({ type: "GET", url: "/Home/anaKategori", success: function (data) { . . .**************************** Intente dar la extensión del archivo como aquí en url
$.ajax({ url : 'test.php', //like this type : 'post', data : {'data':'1'}, . . .Espero que haya obtenido los resultados con esta solución, gracias.