public async Task <IActionResult> GetRandomTree() { var total = await _dbContext.Accounts.CountAsync(); var rand = new Random(); var selection = _dbContext.Accounts.OrderBy(a => a.Id).Skip(rand.Next(0, total)); Account root = await selection.FirstOrDefaultAsync(); TreeBuilder treeBuilder = new TreeBuilder(_dbContext); var account = treeBuilder.BuildTreeInMemory(root); DefaultContractResolver contractResolver = new DefaultContractResolver { NamingStrategy = new CamelCaseNamingStrategy() }; string json = JsonConvert.SerializeObject(account, new JsonSerializerSettings { ContractResolver = contractResolver, Formatting = Formatting.Indented }); //Console.WriteLine(json); ViewData["total"] = await selection.CountAsync(); ViewData["first"] = root.Id; return(View("GetTree", json)); }
public async Task <IActionResult> GetTree(int?parentId) { Account root = parentId.HasValue ? await _dbContext.Accounts.FindAsync((long)parentId) : await _dbContext.Accounts.OrderBy(a => a.Id).FirstOrDefaultAsync(); if (root == null) { return(View("GetTree")); } TreeBuilder treeBuilder = new TreeBuilder(_dbContext); var account = treeBuilder.BuildTreeInMemory(root); DefaultContractResolver contractResolver = new DefaultContractResolver { NamingStrategy = new CamelCaseNamingStrategy() }; string json = JsonConvert.SerializeObject(account, new JsonSerializerSettings { ContractResolver = contractResolver, Formatting = Formatting.Indented }); //Console.WriteLine(json); return(View("GetTree", json)); }