简体   繁体   English

带有MVC Sitemap的Breadcrumb只显示一个级别

[英]Breadcrumb with MVC Sitemap shows only one level

I need to use MVCSitemap provider in a web application, but I can't make it work where I have dynamic urls. 我需要在Web应用程序中使用MVCSitemap提供程序,但我无法在有动态URL的地方使用它。

I have a list of categories, which can have parents and children. 我有一个类别列表,可以有父母和孩子。 For example if I click on a category, the breadcrumb looks like this: 例如,如果我点击一个类别,面包屑看起来像这样:

Home > Filter

if I click on a children of the Filter, I got: 如果我点击过滤器的孩子,我得到:

Filter > Air filter

the Home link disappears. 主页链接消失。 If I click on the childrens of the "Air filter" I got: 如果我点击“空气过滤器”的孩子,我得到:

Air filter > air filter children

and so on. 等等。 Always the two last levels are showed, and if I click on the first level, always goes back to the Home page. 始终显示最后两个级别,如果我单击第一个级别,则始终返回主页。

This is in my MvcSitemap: 这是在我的MvcSitemap中:

 <mvcSiteMapNode title="Home" controller="Home" action="Index">
 <mvcSiteMapNode title="Product" controller="Product" action="SubCategories" preservedRouteParameters="selected,category,id,engineId">
    <mvcSiteMapNode title="Details" controller="Product" action="ProductDetails" preservedRouteParameters="supplierName,code,name,prodId,-1"/>
  </mvcSiteMapNode>

  </mvcSiteMapNode> 

This is the Subcategories method from Product controller: 这是Product controller的Subcategories方法:

 [MvcSiteMapNode(Title = "Article", ParentKey = "SubCategories")]
 [Route("{selected}-{category}-{id}-{engineId}")]
 public ActionResult SubCategories(string selected, string category, int id, string engineId)
    {  
         ...........................  
        SiteMaps.Current.CurrentNode.Title = categoryName;
        if(categoryRepository.GetCategoryByID(id).ParentId.HasValue)
        {
            int parentId = categoryRepository.GetCategoryByID(id).ParentId.Value;
            string parentName = categoryRepository.GetCategoryByID(parentId).Name;
            SiteMaps.Current.CurrentNode.ParentNode.RouteValues["id"] = id;
            SiteMaps.Current.CurrentNode.ParentNode.Title = parentName;
        }

Can you please help me, what I'm doing wrong here? 你能帮帮我吗,我在这里做错了什么? I checked all the explanations on the web, I tried in many ways, but none of them resolved this problem. 我检查了网上的所有解释,我尝试了很多方面,但没有一个解决了这个问题。

Here is my sample. 这是我的样本。 You can refer it. 你可以参考一下。

The first, I have a sitemap: 第一个,我有一个站点地图:

 <mvcSiteMapNode title="Home" controller="Home" action="Index">
        <mvcSiteMapNode title="About" controller="Home" action="About" />
        <mvcSiteMapNode title="Contact" controller="Home" action="Contact" />
        <mvcSiteMapNode title="Administration" clickable="false">
          <mvcSiteMapNode title="User Mgmt" controller="Administration" action="UserMgmt" clickable="false" >
            <mvcSiteMapNode title="List Role" controller="Administration" action="ListRole" >
              <mvcSiteMapNode title="Details" controller="Administration" action="Details">
                <mvcSiteMapNode title="XXX" controller="Administration" action="XXX" />
              </mvcSiteMapNode>
            </mvcSiteMapNode>
          </mvcSiteMapNode>
          <mvcSiteMapNode title="Role Mgmt" controller="Home" action="RoleMgmt" />    
        </mvcSiteMapNode>    
      </mvcSiteMapNode>

//And then, I create a BootstrapMenuHelperModel view that load this sitemap. //I putted it at DisplayTemplates. 

@model MvcSiteMapProvider.Web.Html.Models.MenuHelperModel
@using MvcSiteMapProvider.Web.Html.Models

@helper  TopMenu(List<SiteMapNodeModel> nodeList)
{
    <nav class="navbar navbar-default" role="navigation">
        <div class="container-fluid">
            <div class="collapse navbar-collapse">
                <ul class="nav navbar-nav">
                    @foreach (SiteMapNodeModel node in nodeList)
                    {
                        string url = node.IsClickable ? node.Url : "#";

                        if (!node.Children.Any())
                        {
                            <li><a href="@url">@node.Title</a></li>
                        }
                        else
                        {
                            <li class="dropdown">
                                <a class="dropdown-toggle" data-toggle="dropdown">@node.Title <span class="caret"></span></a>
                                @DropDownMenu(node.Children)
                            </li>
                        }

                        if (node != nodeList.Last())
                        {
                            <li class="divider-vertical"></li>
                        }
                    }
                </ul>
            </div>
        </div>
    </nav>
}

@helper DropDownMenu(SiteMapNodeModelList nodeList)
{
    <ul class="dropdown-menu" role="menu">
        @foreach (SiteMapNodeModel node in nodeList)
        {
            if (node.Title == "Separator")
            {
                <li class="divider"></li>
                continue;
            }

            string url = node.IsClickable ? node.Url : "#";

            if (!node.Children.Any())
            {
                <li><a href="@url">@node.Title</a></li>
            }
            else
            {
                <li class="dropdown-submenu"><a href="@url">@node.Title</a>@DropDownMenu(node.Children)</li>
            }
        }
    </ul>
}

@TopMenu(Model.Nodes)

//Finally, call this view from layout
<div class="row">
            <div class="span12">
                <nav>
                    @Html.MvcSiteMap().Menu("BootstrapMenuHelperModel")
                </nav>
            </div>
        </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM