与动态参数一起使用

与动态参数一起使用

本文介绍了如何将Html.MvcSiteMap().Menu()与动态参数一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在使用MvcSiteMapProvider 4.6.3,MVC 4.

I am using MvcSiteMapProvider 4.6.3, MVC 4.

我想使用Html.MvcSiteMap().Menu()生成菜单.

I want to use Html.MvcSiteMap().Menu() to generate the menu.

问题是我的URL中有多个参数,这些参数会根据用户和文档ID进行更改.

The problem is I have multiple parameters in the URLs, which is changed based on user and document id.

我的站点地图如下:

<mvcSiteMapNode title="Home" controller="Home" action="Index">
  <mvcSiteMapNode title="Site Map Test" controller="SitemapTest" action="Index" area="" key="sitemaptestnode">
    <mvcSiteMapNode title="Sub1" controller="SitemapTest" action="Sub1" area="" />
    <mvcSiteMapNode title="Sub2" controller="SitemapTest" action="Sub2" area="" />
  </mvcSiteMapNode>
</mvcSiteMapNode>

这些动作需要不同的参数.

These actions need different parameters.

    public ActionResult Index(string id, string clientId)
    {
        return View();
    }

    public ActionResult Sub1(string id, string productId)
    {
        return View();
    }

    public ActionResult Sub2(string id, string staffId)
    {
        return View();
    }

我尝试使用HTML帮助程序来生成菜单.

I try to use the Html helper to generate the Menu.

@Html.MvcSiteMap().Menu(node, false, false)

所以,如果我在索引"页面上.菜单应该是

So if I am on the Index page. And the Menu should be

SitemapTest/Index/product1?clientId = clientId2

SitemapTest/Index/product1?clientId=clientId2

SitemapTest/Index/client1?productId = product2

SitemapTest/Index/client1?productId=product2

SitemapTest/Index/client1?staffId = staffId1

SitemapTest/Index/client1?staffId=staffId1

这些ID是从数据库获取的.

These ids are getting from database.

是否可以将参数传递给HTML帮助以指定用于每个链接的ID?

Is there a way to pass the parameter to the Html help to specify which Id to use for each link?

推荐答案

有两种方法,这取决于路由值是直接用于识别页面还是环境值.

There are 2 ways, and it depends on whether the route values are directly used to identify page, or are some ambient value.

对于直接标识页面的值,您应该使用 IDynamicNodeProvider 或ISiteMapNodeProvider,以便在将新记录添加到数据库时它们可以构建新节点.

For values that directly identify the page, you should configure the nodes using IDynamicNodeProvider or ISiteMapNodeProvider so they can build new nodes when new records are added to the database.

public class ProductDynamicNodeProvider
    : DynamicNodeProviderBase
{
    public override IEnumerable<DynamicNode> GetDynamicNodeCollection(ISiteMapNode node)
    {
        // TODO: Replace MyEntities with your entity framework context object
        using (var db = new MyEntities())
        {
            // Create a node for each album
            foreach (var album in db.Products)
            {
                DynamicNode dynamicNode = new DynamicNode();
                dynamicNode.Title = product.Name;
                dynamicNode.ParentKey = "Products"; // There must be a node in the SiteMap with key set to "Products" for this to work

                dynamicNode.PreservedRouteParameters.Add("id"); // Force a match on this parameter always
                dynamicNode.RouteValues.Add("productId", product.Id);

                yield return dynamicNode;
            }
        }
    }
}

对于动态节点提供程序,您需要一个定义节点,该定义节点将在创建节点时用作模板,而实际上不会在SiteMap中存在.

And for dynamic node providers, you need a definition node that will be used as a template when creating the nodes, and it will not actually exist in the SiteMap.

// This node will be added to the SiteMap and serves as the parent node of each product
<mvcSiteMapNode title="All Products" controller="Products" action="Index" area="" key="Products">
    // This node will become a template for each product node, but the node itself won't be added to the SiteMap
    <mvcSiteMapNode controller="Products" action="Details" area="" dynamicNodeProvider="MyNamespace.ProductDynamicNodeProvider, MyAssemblyName" />
</mvcSiteMapNode>

对于环境值(用户ID,会话ID等),您可以通过将SiteMap配置为keepedRouteParameters来强制SiteMap始终匹配它们.

For ambient values (userId, sessionId, etc.), you can force the SiteMap to always match them by configuring them as preservedRouteParameters.

<mvcSiteMapNode title="Sub2" controller="SitemapTest" action="Sub2" area="" preservedRouteParameters="clientId,productId,staffId,id" />

您还可以通过强制匹配某些参数并为其他参数的组合提供节点实例来结合这两种技术.

You can also combine these 2 techniques by forcing a match on certain parameters and providing node instances for combinations of other parameters.

要深入了解这些选项,请阅读如何使MvcSiteMapProvider记住用户的位置.

For an in depth look at these options, read How to Make MvcSiteMapProvider Remember a User's Position.

这篇关于如何将Html.MvcSiteMap().Menu()与动态参数一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 09:49