Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Subnav and Submenu

Vote:
 

Hi All,

We are doing mikgration from DNN to EPiserver while doing migration main navigation is coming but subnav and submenu not loading. what is the steps to follow?

Mainnav code:

 @Html.MenuList(SiteDefinition.Current.StartPage,
                                @<li class="@(item.Selected ? "active" : null)">
                                    @Html.PageLink(item.Page, null, new { @class = string.Join(" ", item.Page.GetThemeCssClassNames()) })
                                </li>)

#254020
Apr 28, 2021 10:06
Vote:
 

I imagine this helper is only built for a single level, you'd have to create your own navigation code if you want to go multiple levels.

#254023
Apr 28, 2021 11:22
Vote:
 

Here's some example recursive naviagation code for when you want to build a nested menu. You can then pass this through to razor script and write your markup round it

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using EPiServer;
using EPiServer.Core;
using EPiServer.Filters;

namespace Appius.EPI.Core.Helpers
{
    /// <summary>
    /// Helper for working with core navigation
    /// </summary>
    public class NavigationHelper
    {
        private readonly IContentLoader _ContentLoader;

        /// <summary>
        /// Initializes a new instance of the <see cref="NavigationHelper"/> class.
        /// </summary>
        public NavigationHelper(IContentLoader ContentLoader)
        {
            _ContentLoader = ContentLoader;
        }

        /// <summary>
        /// Gets the navigation for the core pages.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ParentPageContentReference">The parent page content reference.</param>
        /// <param name="IncludeParentPage">if set to <c>true</c> [include parent page].</param>
        /// <param name="Level">The level.</param>
        /// <returns>The navigation</returns>
        public IList<NavigationViewModel> GetNavigation<T>(ContentReference ParentPageContentReference, bool IncludeParentPage = true, int Level = 1) where T: PageData, INavigationPage
        {
            var pageList = new List<NavigationViewModel>();
            var currentPage = PageExtensions.GetCurrentPage();

            if (IncludeParentPage)
            {
                var parentPageModel = _ContentLoader.Get<T>(ParentPageContentReference);
                pageList.Add(PopulateNavigationViewModel<T, NavigationViewModel>(parentPageModel, currentPage));
            }

            var recursiveNavigation = GetRecursiveNavigation<T>(ParentPageContentReference, currentPage, Level, 1);
            pageList.AddRange(recursiveNavigation);

            return pageList;
        }

        /// <summary>
        /// Gets the recursive navigation.
        /// </summary>
        /// <typeparam name="T">The type of the page</typeparam>
        /// <param name="PageContentReference">The page content reference.</param>
        /// <param name="CurrentPage">The current page.</param>
        /// <param name="Level">The level we want to go to.</param>
        /// <param name="CurrentLevel">The current level.</param>
        /// <returns></returns>
        private IList<NavigationViewModel> GetRecursiveNavigation<T>(ContentReference PageContentReference, PageData CurrentPage, int Level, int CurrentLevel) where T : PageData, INavigationPage
        {
            var pageList = new List<NavigationViewModel>();
            var childPages = _ContentLoader.GetChildren<T>(PageContentReference);
            var filteredChildPages = FilterForVisitor.Filter(childPages).Cast<T>().Where(p => p.VisibleInMenu);

            foreach (var filteredChildPage in filteredChildPages)
            {
                var viewModel = PopulateNavigationViewModel<T, NavigationViewModel>(filteredChildPage, CurrentPage);

                if (Level > CurrentLevel)
                {
                    viewModel.ChildNavigation = GetRecursiveNavigation<T>(viewModel.Reference, CurrentPage, Level, CurrentLevel + 1);
                    viewModel.IsDescendantNavigationCurrentPage = viewModel.ChildNavigation.Any(cn => cn.IsCurrentPage);
                }

                pageList.Add(viewModel);
            }

            return pageList;
        }

        /// <summary>
        /// Populates the navigation view model.
        /// </summary>
        /// <typeparam name="TPageModel">The type of the page</typeparam>
        /// <typeparam name="TNavigationViewModel">The type of the navigation view model.</typeparam>
        /// <param name="PageModel">The page model.</param>
        /// <param name="CurrentPage">The current page.</param>
        /// <returns>The navigation view model</returns>
        private TNavigationViewModel PopulateNavigationViewModel<TPageModel, TNavigationViewModel>(TPageModel PageModel, PageData CurrentPage)
            where TPageModel : PageData, INavigationPage
            where TNavigationViewModel : NavigationViewModel
        {
            var model = Activator.CreateInstance<TNavigationViewModel>();

            model.Name = PageModel.Title;
            model.Url = ContentExtensions.GetUrl(PageModel.ContentLink, false);
            model.IsCurrentPage = CurrentPage.PageLink.ID == PageModel.ContentLink.ID;
            model.Reference = PageModel.ContentLink;

            return model;
        }
    }
}
#254024
Apr 28, 2021 11:26
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.