Search code examples
c#blazorsyncfusion

Razor/Syncfusion: On async page loading, treedata won't populate unless I make an action


So I am writing this application in razor and right now I am doing a custom navigation bar. Now, the problem is in the OnInitialized event that I wrote:

 protected async override void OnInitialized()
    {
        base.OnInitialized();
        var user = (await AuthStat).User;
        if (user.Identity.IsAuthenticated)
        {
            await Authenticated();
        }
        else
        {
            await NotAuthenticated();
        }
    }

Where the functions are:

 protected Task Authenticated()
    {
        treedata = null;
        treedata = new List<TreeData>();
        treedata.Add(new TreeData { nodeId = "01", nodeText = "Acasa", iconCss = "icon-microchip icon" });
        treedata.Add(new TreeData { nodeId = "03", nodeText = "Logout", iconCss = "icon-thumbs-up-alt icon" });
        treedata.Add(new TreeData { nodeId = "04", nodeText = "Vremea", iconCss = "icon-th icon" });
        treedata.Add(new TreeData { nodeId = "05", nodeText = "Profil", iconCss = "icon-code icon" });
        treedata.Add(new TreeData { nodeId = "06", nodeText = "Calcul Salariu", iconCss = "icon-chrome icon" });
        treedata.Add(new TreeData { nodeId = "07", nodeText = "Mijloace Fixe", iconCss = "icon-up-hand icon" });
        return Task.CompletedTask;

    }

    protected Task NotAuthenticated()
    {
        treedata = null;
        treedata = new List<TreeData>();
        treedata.Add(new TreeData { nodeId = "01", nodeText = "Acasa", iconCss = "icon-microchip icon" });
        treedata.Add(new TreeData { nodeId = "02", nodeText = "Login", iconCss = "icon-docs icon" });
        treedata.Add(new TreeData { nodeId = "04", nodeText = "Vremea", iconCss = "icon-th icon" });
        treedata.Add(new TreeData { nodeId = "06", nodeText = "Calcul Salariu", iconCss = "icon-chrome icon" });
        return Task.CompletedTask;
    }

The variable treedata is:

SfTreeView<TreeData> TreeView;
   public class TreeData
   {
       public string nodeId { get; set; }
       public string nodeText { get; set; }
       public string iconCss { get; set; }
       public bool hasChild { get; set; }
       public string pid { get; set; }
   }

My problem is the following: Whenever the page loads, the treedata will populate only when an action occur (ie. search or toggle the navigation menu) and I want to show when the page load, but I don't know how.


Solution

  • //protected async override void OnInitialized()
      protected override async Task OnInitializedAsync()
      {
        // base.OnInitialized();
        // ...  like before
    

    An async void method is not (cannot be) awaited. Your Authenticated/NotAuthenticated methods run out of sync with the rendering.