在mvc5中使用MvcSiteMap创建面包屑

时间:2014-11-22 11:06:20

标签: c# asp.net-mvc asp.net-mvc-5 mvcsitemapprovider

我想在mvc5中使用MvcSiteMap创建面包屑。 我写下面的代码。 但我希望当我点击第一,第二,......他们的Id传递给View。 但它不起作用。我做对了吗?

//Controller Name=News

Home
News
     first     //id=1
     second   //id=2
     third   // id=3
About


<mvcSiteMapNode title="Home" controller="Home" action="Index">
<mvcSiteMapNode  title="News" controller="News" action="Index" key="News">
</mvcSiteMapNode>
<mvcSiteMapNode title="About" controller="About" action="Index"/>

   [MvcSiteMapNode(Title = "News", ParentKey = "News")]
    public ActionResult News(int id)
    {
        ViewBag.id = id;
        return View();
    }

1 个答案:

答案 0 :(得分:2)

the documentation中所述,您必须明确配置所有自定义路由参数(包括ID)。您可以通过在PreservedRouteParameters中包含键来创建与操作方法的1对1关系,也可以通过为每个路径值组合创建单独的节点来创建与该操作的1对多关系。

1对1关系

使用XML

<mvcSiteMapNode title="News" controller="News" action="News" preservedRouteParameters="id"/>

或使用.NET Attributes

[MvcSiteMapNode(Title = "News", ParentKey = "News", PreservedRouteParameters = "id")]
public ActionResult News(int id)
{
    ViewBag.id = id;
    return View();
}
  

注意:使用此方法,URL只能在SiteMapPath HTML帮助程序中正确解析,您可能需要手动将节点的标题和可见性修复为explained here

1对多关系

使用XML

<mvcSiteMapNode title="Article 1" controller="News" action="News" id="1"/>
<mvcSiteMapNode title="Article 2" controller="News" action="News" id="2"/>
<mvcSiteMapNode title="Article 3" controller="News" action="News" id="3"/>

或使用.NET Attributes

[MvcSiteMapNode(Title = "Article 1", ParentKey = "News", Attributes = @"{ ""id"": 1 }")]
[MvcSiteMapNode(Title = "Article 2", ParentKey = "News", Attributes = @"{ ""id"": 2 }")]
[MvcSiteMapNode(Title = "Article 3", ParentKey = "News", Attributes = @"{ ""id"": 3 }")]
public ActionResult News(int id)
{
    ViewBag.id = id;
    return View();
}

或使用Dynamic Node Provider

使用XML中的定义节点:

<mvcSiteMapNode  title="News" controller="News" action="Index" key="News">
    // Setup definition node in XML (won't be in the SiteMap)
    // Any attributes you put here will be the defaults in the dynamic node provider, but can be overridden there.
    <mvcSiteMapNode dynamicNodeProvider="MyNamespace.NewsDynamicNodeProvider, MyAssembly" controller="News" action="News"/>
</mvcSiteMapNode>

或.NET属性中的定义节点:

// Setup definition node as a .NET Attribute (won't be in the SiteMap)
// Any properties you put here will be the defaults in the dynamic node provider, but can be overridden there.
[MvcSiteMapNode(DynamicNodeProvider = "MyNamespace.NewsDynamicNodeProvider, MyAssembly")]
public ActionResult News(int id)
{
    ViewBag.id = id;
    return View();
}

动态节点提供程序实现(上述两个定义节点之一所需):

public class NewsDynamicNodeProvider 
    : DynamicNodeProviderBase 
{ 
    public override IEnumerable<DynamicNode> GetDynamicNodeCollection(ISiteMapNode node) 
    {
        using (var db = new EnityContext())
        {
            // Create a node for each news article 
            foreach (var news in db.News) 
            { 
                var dynamicNode = new DynamicNode(); 
                dynamicNode.Title = news.Title;
                dynamicNode.ParentKey = "News";
                dynamicNode.RouteValues.Add("id", news.Id);

                yield return dynamicNode;
            }
        }
    } 
}