0

I have been wondering how to seed the left menu with items from an API. Let's say a user logs in, and I want to load their lesson list and interact with it. My questions are:

1.How do I seed the menu from the API? A small code snippet would be appreciated. Atm it seems that it only allows to seed manually? 2.How can we interact with the menu items? For example, each nav item will carry a lesson GUID that, when clicked upon, will load the corresponding lesson content(need to attach some click event?).

1 Answer 1

1

You can use MenuContributors to manage menu items dynamically.

You can access any service by using context.ServiceProvider in the ConfigureMenuAsync() method and get menu items from wherever you want and add them to the application menu.

    public async Task ConfigureMenuAsync(MenuConfigurationContext context)
    {
        if (context.Menu.Name == StandardMenus.Main)
        {
            // Access any service by using ServiceProvider in the context 👇
            var fooService = context.ServiceProvider.GetRequiredService<IFooService>();

            // You can call an async operation here. Even you can get them from database.
            var fooMenus = await fooService.GetMenusAsync();

            foreach (var item in fooMenus)
            {
                context.Menu.Items.Add(new ApplicationMenuItem(
                    item.Id.ToString(),
                    item.Name,
                    // 👇 You can handle it on a page with URL /foo/bar/{Id}
                    "/foo/bar/" + item.Id)); 
            }
        }
    }

By default 'YourAppNameMenuContributor' is already placed in your project. You can use it instead creating new one.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.