动态更改父密钥-IDynamicNodeProvider MVC [英] change parent Key dynamically - IDynamicNodeProvider MVC

查看:53
本文介绍了动态更改父密钥-IDynamicNodeProvider MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我根据此 https://github实施IDynamicNodeProvider.com/maartenba/MvcSiteMapProvider/wiki/Defining-sitemap-nodes-using-IDynamicNodeProvider

我想动态更改父键.根据我的菜单导航,我有很多父节点.一些例子是

I would like to change the parent Key dynamically. I have a lot of parent nodes according to my menu navigation. Some example is

1) Home>Profile>Quality Policy
2) Home>eServices>eService 

我有两个桌子.在第一个菜单中,我保留菜单.MenuID,MenuTitle,并在另一个中存储我的内容.ArticleID,ArticleTitle,ArticleContent,MenuID.

I have two tables. In the first one I keep my menus. MenuID, MenuTitle and in the other I store my content. ArticleID, ArticleTitle, ArticleContent, MenuID.

我需要动态更改父键.

这是我的课程

      public override IEnumerable<DynamicNode>   GetDynamicNodeCollection(ISiteMapNode node)

    { 
        webdata storeDB = new webdata();

        var returnValue = new List<DynamicNode>();


        foreach (var article in storeDB.SiteContents) 
        {


            DynamicNode enode = new DynamicNode();
            enode.Title = article.ArticleTitle;

            enode.ParentKey = "?";
            //Specify Controller and Action name
            enode.Controller = "SiteContents";
            enode.Action = "ArticleDetails";
            enode.RouteValues.Add("id", article.ArticleID);
            enode.PreservedRouteParameters.Add("slug");
            returnValue.Add(enode);


            yield return enode;
        }


    }

我的站点地图文件

<mvcSiteMapNode title="Home" controller="Home" action="Index">

 <mvcSiteMapNode  controller="SiteContents"   dynamicNodeProvider="Myproject.Models.ElkeDynamicNodeProvider, Myproject"  />
 </mvcSiteMapNode>

我如何连接两个表,以便将MenuTitle用作parentKey,以及如何在正确的操作中将其链接起来.谢谢

How can I join the two tables so to bring the MenuTitle as a parentKey and how this will be linked at the right action. Thank you

推荐答案

每个节点不能有一个以上的父级,也不能动态更改父级键.

It is not possible to have more than one parent per node or to change the parent key dynamically.

我不确定您到底要达到什么目的,但是请注意,可能有

I am not sure exactly what you are trying to achieve by this, but do note that it is possible to have multiple navigation paths to a page by providing 2 unique URLs to that page and using canonicalKey or canonicalUrl on one of them to prevent search engines from penalizing you.

还要注意,实际上您可以使用数据库中主键和/或外键的变体来创建节点.因此,如果您的数据库中有关系,则可以基于该关系创建动态节点.

Also note that you could in fact use a variation of the primary and/or foreign keys in your database to create the nodes. So if you have the relationship in your database, you can just create dynamic nodes based on that same relationship.

例如,如果您在 Article 表中有一个 MenuID (或可以将这些表联接以获得相应的 MenuID ,需要遍历每个表并基于 MenuID (例如 MenuID1234 )创建一个键,该键用于菜单实体的键和文章的父键实体.那么每个文章都会有一个 ArticleID3456 密钥,可用作下一级的父密钥.

For example, if you have a MenuID in your Article table (or can join the tables to get a corresponding MenuID, you just need to loop through each table and create a key based off of MenuID (such as MenuID1234) that is used for the key of the menu entity and the parent key of the article entity. Each article would then have a key ArticleID3456, which could be used as a parent key of the next level.

public override IEnumerable<DynamicNode>   GetDynamicNodeCollection(ISiteMapNode node)
{ 
    // I am assuming here that webdata is your 
    // Entity Framework context class. If so,
    // you should wrap it in a using block.
    using (webdata storeDB = new webdata())
    {
        // Loop through your Menu Table
        foreach (var menuItem in storeDB.Menu) 
        {
            DynamicNode enode = new DynamicNode();
            enode.Title = menuItem.MenuTitle;

            // I am assuming that you want your menu categories below the home page
            // and that that node has a key="Home" attribute set.
            enode.ParentKey = "Home";

            // Specify the key based on the primary key in the DB
            // Cast to a string if necessary.
            enode.Key = "MenuID" + menuItem.MenuID;

            // Specify Controller and Action name
            // (not sure where you want to navigate to here)
            enode.Controller = "SiteContents";
            enode.Action = "ArticleDetails";
            enode.RouteValues.Add("id", menuItem.MenuID);

            // If your menu categories don't represent
            // real pages, you can use non-clickable nodes
            // instead of controller, action, and id.
            //enode.Clickable = false;

            // Add this if you use a slug on these pages
            enode.PreservedRouteParameters.Add("slug");

            yield return enode;
        }

        // Loop through your Article Table
        foreach (var article in storeDB.Article) 
        {
            DynamicNode enode = new DynamicNode();
            enode.Title = article.ArticleTitle;

            // Attach to your foreign key, the same way as in your DB
            enode.ParentKey = "MenuID" + article.MenuID;

            // Give your article a sensible unique key.
            // This will save some memory and make it easy
            // to add a level below the article if needed.
            enode.Key = "ArticleID" + article.ArticleID;

            //Specify Controller and Action name
            enode.Controller = "SiteContents";
            enode.Action = "ArticleDetails";
            enode.RouteValues.Add("id", article.ArticleID);
            enode.PreservedRouteParameters.Add("slug");

            yield return enode;
        }
    }
}

这篇关于动态更改父密钥-IDynamicNodeProvider MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆