Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Menus from database #8557


User avatar
0
fawad29 created

I would like to create menu from the items stored in the database, instead of hardwiring them in AppNavigationProvider.cs. Do you have a sample example for this purpose? What should be the database look like for this purpose and in what format menus should be returned to abp.nav.menus.MainMenu?

I am using ANZ v8 MVC Core and Jquery.


11 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    This is inconsistent with the design of the framework, You may need to change a lot of framework built-in components.The menu may depend on permissions or features.

    If you only want to create some dynamic menus, you can add it to the current function, read these menus from the database, and display them in the UI, so that it will not change the design of the framework.

  • User Avatar
    0
    fawad29 created

    Do you mean I should get menu from database in SetNavigation function of AppNavigationProvider1 as shown below?

    public override void SetNavigation(INavigationProviderContext context)
            {
                var menu = context.Manager.Menus[MenuName] = new MenuDefinition(MenuName, new FixedLocalizableString("Main Menu"));
                
                var myList = _myMenuService.GetMenusByMyParameter(_mySession.myParameter);
                
                foreach(MyMenuItem myMenuItem in myList)
                {
                   menu.AddItem(new MenuItemDefinition(
                            myMenuItem.Text,
                            L(myMenuItem.Text),
                            url: myMenuItem.url,
                            icon: myMenuItem.icons,
                            permissionDependency: new SimplePermissionDependency(myMenuItem.permissionString)
                        )
                }
                
             }
    
  • User Avatar
    0
    maliming created
    Support Team

    XXXNavigationProvider will only read the menu when the application starts, if this is what you need you can read the database in the SetNavigation method.

  • User Avatar
    0
    fawad29 created

    Do we have access to AbpSession.UserId in SetNavigation method?

    Yes, I just need to get the menu from the database and display them with any permission dependcies as well. Is this a suitable placce?

  • User Avatar
    0
    maliming created
    Support Team

    NavigationProvider will be initialized when the application starts. Although you can inject IAbpSession, it may have no value.

  • User Avatar
    0
    fawad29 created

    So what would be the best place to read menu from the database? I would need a value from session to retreive menu from the database. You mentioned current function in previous reply where is this function? Please reply comprehensively if possible with example so that I can close this ticket quickly. Thanks

  • User Avatar
    0
    maliming created
    Support Team

    You can combine Abp's menu(_userNavigationManager.GetMenuAsync) with your dynamic menu in View.

    It's easiest to get extra menus this way.

  • User Avatar
    0
    fawad29 created

    Are you referring to following code in AppMenuViewComponent

    var model = new MenuViewModel
                {
                    Menu = await _userNavigationManager.GetMenuAsync(AppNavigationProvider.MenuName, _abpSession.ToUserIdentifier()),
                    CurrentPageName = currentPageName
                };
    

    How will the menu order be handled? Can I recreate Abp's menu in my database and then just get all menu from the database instead of getting Abp's from navigationManager and then appending mine from the database?

  • User Avatar
    0
    maliming created
    Support Team

    Yes, and HeaderViewComponent.

    You cannot change the design of ABP. Of course you can design your own menu system. Read the menu from the database.

    How will the menu order be handled?

    https://github.com/aspnetzero/aspnet-zero-core/issues/3013

  • User Avatar
    0
    fawad29 created

    I am unable to access following link, I am logged in with my credentials to aspnetzero support. https://github.com/aspnetzero/aspnet-zero-core/issues/3013

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    You need to add your GitHub user on https://aspnetzero.com/LicenseManagement.