MvcSiteMapProvider hidden mvcSiteMapNode with CRUD operation

六眼飞鱼酱① 提交于 2019-12-11 13:37:30

问题


In a project, i've implemented MvcSiteMapProvider that work great. This is a side menu generated with @Html.MvcSiteMap().Menu()

Here is a node of the menu (Mvc.sitemap file).

<mvcSiteMapNode title="Home" controller="Home" action="Index">
<mvcSiteMapNode title="About" controller="Home" action="About"/>
<mvcSiteMapNode title="Project" controller="Home" action="DummyAction">
  <mvcSiteMapNode title="List" controller="Home" action="Project"/>
  <mvcSiteMapNode title="Edit" controller="Home" action="Edit" preservedRouteParameters="id" visibility="SiteMapPathHelper,!*"/>
</mvcSiteMapNode>

When I load the "List" from "Project", it displays a page with all projects and I can select one of them to load the Edit action with the related ID of the project.

The problem is, when I'm in the Edit action page, the side menu is all collapsed, but I'm expecting to have the "Project" node opened. If I add the node "Edit" in the menu, it work (matching node action), but I don’t want this node because its useless for the user.

Also, I've tried the DefaultSiteMapNodeVisibiltyProvider, I can hide the "Edit" node if added, but when I'm in the "Edit" action page, the Project node is also closed.

I'll face the same problem for the "New/Add" operation that I don’t want to see in the side menu, but will be accessible from a link in the project list. However, for these operations, I want to let the user know that it is inside the "Project" section, with the "Project" node opened.

See this project on GitHub: Project on Github

Best regards,


回答1:


Your issue appears to be that you haven't accounted for the recursive nature of the Menu HTML helper. There are 3 templates that the Menu interacts with:

  1. MenuHelperModel.cshtml
  2. SiteMapNodeModelList.cshtml
  3. SiteMapNodeModel.cshtml

The problem is that you haven't added the appropriate logic to the SiteMapNodeModelList.cshtml. In addition, the logic in the MenuHelperModel.cshtml is overly complex.

A good approach for dealing with both a Menu and SiteMapPath HTML helper on the same page with customizations is to use named templates rather than editing the default templates.

BootstrapMenuHelperModel.cshtml

@model MvcSiteMapProvider.Web.Html.Models.MenuHelperModel
@using System.Web.Mvc.Html
@using MvcSiteMapProvider.Web.Html.Models
<ul id="menu" class="nav sidebar-menu">
    @foreach (var node in Model.Nodes)
    {
        string nodeclass = "";
        if (node.IsInCurrentPath && !node.IsRootNode)
        {
            nodeclass = "active";
            if (node.Children.Any())
            {
                nodeclass += " open";
            }
        }

        <li class="@nodeclass">
            @Html.DisplayFor(m => node)
            @if (node.Children.Any())
            {
                // Here we refer to a named template BootstrapMenuNodeModelList.cshtml
                @Html.DisplayFor(m => node.Children, "BootstrapMenuNodeModelList")
            }
        </li>
    }
</ul>

BootstrapMenuNodeModelList.cshtml

@model MvcSiteMapProvider.Web.Html.Models.SiteMapNodeModelList
@using System.Web.Mvc.Html
@using MvcSiteMapProvider.Web.Html.Models
<ul>
    @foreach (var node in Model)
    {
        string nodeclass = "";
        if (node.IsInCurrentPath && !node.IsRootNode)
        {
            nodeclass = "active";
            if (node.Children.Any())
            {
                nodeclass += " open";
            }
        }

        <li class="@nodeclass">
            @Html.DisplayFor(m => node)
            @if (node.Children.Any())
            {
                // Here we refer to a named template BootstrapMenuNodeModelList.cshtml,
                // which happens to be a recursive call to this template.
                @Html.DisplayFor(m => node.Children, "BootstrapMenuNodeModelList")
            }
        </li>
    }
</ul>

Usage

Here, we tell the Menu HTML helper to use our custom template named BootstrapMenuHelperModel.cshtml.

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

NOTE: You could also create a custom template for the SiteMapNodeModel using the same approach. You would just need to call the overload in the other templates to use your template name.

Example:

Change @Html.DisplayFor(m => node) to @Html.DisplayFor(m => node, "MyTemplate") and create a corresponding file named MyTemplate.cshtml in the /Views/Shared/DisplayTemplates/ folder.



来源:https://stackoverflow.com/questions/36162681/mvcsitemapprovider-hidden-mvcsitemapnode-with-crud-operation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!