MvcSiteMap generating a Menu without messing the breadcumbs

前端 未结 1 1652
無奈伤痛
無奈伤痛 2020-11-30 13:22

I\'m configuring my asp.net mvc 5 app to use MvcSiteMap library. So, far I could successfully configure the breadcumbs.
However, the template for a menu is rather more c

相关标签:
1条回答
  • 2020-11-30 14:09

    Make a template for the MenuHelperModel and give it a custom name, and put it in the /Views/Shared/DisplayTemplates/ folder. Then you can make a template for the SiteMapNodeModel and SiteMapNodeModelList and give them custom names. Copy the contents of MenuHelperModel.cshtml, SiteMapNodeModel.cshtml, and SiteMapNodeModelList.cshtml into your new custom helpers.

    Then, change the overrides in each of the HTML helpers within the templates so they call the custom templates instead of the built-in templates.

    // MyMenu.cshtml
    @* // This template is for the root level *@
    @model MvcSiteMapProvider.Web.Html.Models.MenuHelperModel
    @using System.Web.Mvc.Html
    @using MvcSiteMapProvider.Web.Html.Models
    
    <ul id="menu">
        @foreach (var node in Model.Nodes) { 
            <li>@Html.DisplayFor(m => node, "MyMenuNode") @* <-- // Custom Node Helper Name *@
                @if (node.Children.Any()) {
                    @Html.DisplayFor(m => node.Children, "MyMenuNodeList") @* <-- // Custom Node Helper Name *@
                }
            </li>
        }
    </ul>
    
    
    // MyMenuNodeList.cshtml
    @* // This template is for the descendent lists below the root level *@
    @model MvcSiteMapProvider.Web.Html.Models.SiteMapNodeModelList
    @using System.Web.Mvc.Html
    @using MvcSiteMapProvider.Web.Html.Models
    
    <ul>
        @foreach (var node in Model) { 
            <li>@Html.DisplayFor(m => node, "MyMenuNode") @* <-- // Custom Node Helper Name *@
                @if (node.Children.Any()) {
                    @Html.DisplayFor(m => node.Children, "MyMenuNodeList") @* <-- // Custom Node Helper Name *@
                }
            </li>
        }
    </ul>
    
    
    
    // MyMenuNode.cshtml
    @* // This template is for the node *@
    @model MvcSiteMapProvider.Web.Html.Models.SiteMapNodeModel
    @using System.Web.Mvc.Html
    @using MvcSiteMapProvider.Web.Html.Models
    
    Testing @* <-- // If configured right, Testing will appear before every node *@
    
    @if (Model.IsCurrentNode && Model.SourceMetadata["HtmlHelper"].ToString() != "MvcSiteMapProvider.Web.Html.MenuHelper")  { 
        <text>@Model.Title</text>
    } else if (Model.IsClickable) { 
        if (string.IsNullOrEmpty(Model.Description))
        {
            <a href="@Model.Url">@Model.Title</a>
        }
        else
        {
            <a href="@Model.Url" title="@Model.Description">@Model.Title</a>
        }
    } else { 
        <text>@Model.Title</text>
    }
    

    Then call your root template from the menu.

    @Html.MvcSiteMap().Menu("MyMenu")
    

    You can use this as a starting point, and then make the changes to the views accordingly to output your desired HTML.

    Do note that the SiteMapNodeListHelper template ("MySiteMapNodeList" in this case) recursively calls itself for each successive level of nodes.

    0 讨论(0)
提交回复
热议问题