i want to create breadcrumb with MvcSiteMap in mvc5.
i wrote below code.
but i want to when i click on first , second , ... their Id pass to View.
but it dose not work. i do it right?
//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();
}
As stated in the documentation, you must configure all custom route parameters (including id) explicitly. You can create a 1 to 1 relationship with the action method by including the key in PreservedRouteParameters or you can create a 1 to many relationship with the action by creating a separate node for every route value combination.
1 to 1 Relationship
Using XML:
<mvcSiteMapNode title="News" controller="News" action="News" preservedRouteParameters="id"/>
Or Using .NET Attributes:
[MvcSiteMapNode(Title = "News", ParentKey = "News", PreservedRouteParameters = "id")]
public ActionResult News(int id)
{
ViewBag.id = id;
return View();
}
Note: Using this method, the URLs will only resolve correctly in the SiteMapPath HTML helper, and you may need to fix the title and visibility of nodes manually as explained here.
1 to Many Relationship
Using 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"/>
Or Using .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();
}
Or Using a Dynamic Node Provider:
With Definition Node in 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>
Or With Definition Node in .NET Attributes:
// 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();
}
Dynamic Node Provider Implementation (required for either of the 2 definition nodes above):
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;
}
}
}
}