嵌套级别上的动态节点重复

时间:2015-04-08 09:48:02

标签: c# asp.net-mvc mvcsitemapprovider

我对MVCSiteMapProvider有一个问题如下。

我的MVC.siteMap:

<mvcSiteMapNode title="Home" controller="Home" action="Index" key="home">
    <mvcSiteMapNode title="Login/Register" controller="Login" action="Index"/>

    <mvcSiteMapNode title="Collections" clickable="false" key="nodeCollections">
        <mvcSiteMapNode title="Dynamic Nodes" controller="Collections" action="Index" clickable="true" dynamicNodeProvider="mysite.Utilities.DynamicNodeProvider, mysite.com" />
    </mvcSiteMapNode>

    <mvcSiteMapNode title="Categories" clickable="false" key="nodeCategories">
        <mvcSiteMapNode title="Dynamic Nodes" controller="Products" action="Index" clickable="true" dynamicNodeProvider="mysite.Utilities.DynamicNodeProvider, mysite.com" />
    </mvcSiteMapNode>
</mvcSiteMapNode>

我的DynamicNodeProvider:

        foreach (var collection in collections)
        {
            var node = new DynamicNode
            {
                Title = collection.collection,
                Controller = "Collections",
                Action = "Index",
                ParentKey = "home",
                ChangeFrequency = ChangeFrequency.Weekly
            };
            node.RouteValues.Add("seocollection", collection.seoCollection);
            yield return node;
        }

        ////Get Categories Nodes
        // Create a node for each category 
        foreach (var category in categories)
        {
            var node = new DynamicNode
            {
                //Key = category.category,
                Title = category.category,
                Controller = "Products",
                Action = "Index",
                ParentKey = "home",
                ChangeFrequency = ChangeFrequency.Weekly
            };
            node.RouteValues.Add("seocategory", category.seocategory);
            yield return node2;
        }

我的HtmlHelper:

<div style="text-align: center;">
    @Html.MvcSiteMap().Menu("MenuHelperModel", SiteMaps.Current.FindSiteMapNodeFromKey("home"),true,true)
</div>

虽然这可以正常工作,但我在返回的菜单上获得了重复的结果,即显示:

主页
登录/注册
收藏
收藏A
A类
B类 分类
收藏A
A类
B类

我在MVC.sitemap中绑定不正确吗?

1 个答案:

答案 0 :(得分:1)

  

我在MVC.sitemap中绑定不正确吗?

是。您将获得重复的结果,因为您注册了2次相同的动态节点提供程序。此外,您已指定将每个节点嵌套在主节点下方,这将为您提供平面结构。

首先,要解决嵌套问题,您应为每个嵌套项指定一个不同的父键,而不是&#34; home&#34;。根据您构建XML的方式,我猜你不希望他们在&#34; home&#34;节点

    foreach (var collection in collections)
    {
        var node = new DynamicNode
        {
            Title = collection.collection,
            Controller = "Collections",
            Action = "Index",
            // Specify the node you want this node nested under
            ParentKey = "nodeCollections", 
            ChangeFrequency = ChangeFrequency.Weekly
        };
        node.RouteValues.Add("seocollection", collection.seoCollection);
        yield return node;
    }

    ////Get Categories Nodes
    // Create a node for each category 
    foreach (var category in categories)
    {
        var node = new DynamicNode
        {
            //Key = category.category,
            Title = category.category,
            Controller = "Products",
            Action = "Index",
            // Specify the node you want this node nested under
            ParentKey = "nodeCategories",
            ChangeFrequency = ChangeFrequency.Weekly
        };
        node.RouteValues.Add("seocategory", category.seocategory);
        yield return node2;
    }

然后,要修复复制问题,您需要将1个动态节点分解为2,或者只需在XML中指定一次。请注意,声明动态节点提供程序的位置并不重要 - 父键是将所有节点放在节点层次结构中的适当位置。

<mvcSiteMapNode title="Home" controller="Home" action="Index" key="home">
    <mvcSiteMapNode title="Login/Register" controller="Login" action="Index"/>
    <mvcSiteMapNode title="Collections" clickable="false" key="nodeCollections"/>
    <mvcSiteMapNode title="Categories" clickable="false" key="nodeCategories"/>
    <mvcSiteMapNode dynamicNodeProvider="mysite.Utilities.DynamicNodeProvider, mysite.com" />
</mvcSiteMapNode>

您在XML中指定的值将成为动态节点提供程序中的默认值。如果已在动态节点提供程序中指定它们,则它们不是必需的。

另请注意,如果将1个动态节点提供程序拆分为2,则使用该示例比使用此示例更直观,但从技术角度来看,这不是必需的。