我一直在四处寻找,但我找不到问题的答案。
我想要一个这样的菜单:
当我点击Personnel-item时,我想显示以下内容:
当我单击Home-item时,我将返回显示第一个菜单的索引页面。 单击Holiday-item将显示如下菜单:
我的观点在一个项目中,我的控制器在一个不同的项目中。 我似乎无法让它发挥作用。
这是我的站点地图:
<?xml version="1.0" encoding="utf-8" ?><mvcSiteMap xmlns="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-3.0" enableLocalization="true">
<mvcSiteMapNode title="Home" controller="Home" action="Index">
<mvcSiteMapNode title="Administration" clickable="false">
<mvcSiteMapNode title="Employee" controller="Personnel" action="Index">
<mvcSiteMapNode title="New Employee" controller="Personnel" action="Create"/>
</mvcSiteMapNode>
<mvcSiteMapNode title="Holiday" controller="Holidays" action="Index">
<mvcSiteMapNode title="New Holiday" controller="Holidays" action="HolidayNew"/>
</mvcSiteMapNode>
</mvcSiteMapNode>
</mvcSiteMapNode>
我的web.config:
<siteMap defaultProvider="MvcSiteMapProvider" enabled="true">
<providers>
<clear />
<add name="MvcSiteMapProvider"
type="MvcSiteMapProvider.DefaultSiteMapProvider, MvcSiteMapProvider"
siteMapFile="~/Mvc.Sitemap"
securityTrimmingEnabled="true" cacheDuration="5" enableLocalization="true"
scanAssembliesForSiteMapNodes="true" includeAssembliesForScan=""
excludeAssembliesForScan=""
nodeKeyGenerator="MvcSiteMapProvider.DefaultNodeKeyGenerator, MvcSiteMapProvider"
controllerTypeResolver="MvcSiteMapProvider.DefaultControllerTypeResolver, MvcSiteMapProvider"
actionMethodParameterResolver="MvcSiteMapProvider.DefaultActionMethodParameterResolver, MvcSiteMapProvider"
aclModule="MvcSiteMapProvider.DefaultAclModule, MvcSiteMapProvider"
siteMapNodeUrlResolver="MvcSiteMapProvider.DefaultSiteMapNodeUrlResolver, MvcSiteMapProvider"
siteMapNodeVisibilityProvider="MvcSiteMapProvider.DefaultSiteMapNodeVisibilityProvider, MvcSiteMapProvider"
siteMapProviderEventHandler="MvcSiteMapProvider.DefaultSiteMapProviderEventHandler, MvcSiteMapProvider" />
</providers>
答案 0 :(得分:3)
您将需要修改MVCSiteMap DisplayTemplates - 这使您可以精确控制菜单的显示方式。本教程可能有所帮助:http://edspencer.me.uk/2011/09/20/mvc-sitemap-provider-tutorial-2-breadcrumbs/
<强>更新强>
这是一个让你入门的例子。
<强> Mvc.sitemap 强>
<mvcSiteMapNode title="RootNode" controller="Home" action="Index">
<mvcSiteMapNode title="Home" controller="Home" action="Index"/>
<mvcSiteMapNode title="Administration" clickable="false">
<mvcSiteMapNode title="Personnel" controller="Home" action="Personnel">
<mvcSiteMapNode title="New Personnel" controller="Home" action="PersonnelNew"/>
</mvcSiteMapNode>
<mvcSiteMapNode title="Holiday" controller="Home" action="Holiday">
<mvcSiteMapNode title="New Holiday" controller="Home" action="HolidayNew"/>
</mvcSiteMapNode>
</mvcSiteMapNode>
<mvcSiteMapNode title="About" controller="Home" action="About"/>
</mvcSiteMapNode>
<强> SiteMapHelperModel.cshtml 强>
<ul class="siteMap">
@foreach (var node in Model.Nodes) {
if (node.IsRootNode)
{
foreach (var menu in node.Children)
{
<li>@Html.DisplayFor(m => menu)
@if (menu.IsInCurrentPath || menu.IsCurrentNode)
{
if (menu.Children.Any())
{
@Html.DisplayFor(m => menu.Children)
}
}
else
{
foreach (var child in menu.Children)
{
@Html.DisplayFor(m => child)
}
}
</li>
}
}
}
</ul>
<强> SiteMapNodeModelList.cshtml 强>
<ul>
@foreach (var node in Model) {
if (node.IsCurrentNode)
{
<li>@Html.DisplayFor(m => node)
@foreach (var child in node.Children)
{
@Html.DisplayFor(m => child);
}
</li>
}
else if (node.IsInCurrentPath)
{
<li>@Html.DisplayFor(m => node)
if (node.Children.Any())
{
@Html.DisplayFor(m => node.Children)
}
</li>
}
}
</ul>
<强> SiteMapNodeModel.cshtml 强>
@if (Model.IsClickable) {
<a href="@Model.Url">@Model.Title</a>
} else {
<text>@Model.Title</text>
}
现在,当我回家时,菜单呈现为:
<ul class="siteMap">
<li>
<a href="/">Home</a>
</li>
<li>
Administration
<a href="/Home/Personnel">Personnel</a>
<a href="/Home/Holiday">Holiday</a>
</li>
<li>
<a href="/Home/About">About</a>
</li>
</ul>
当我点击人员菜单项时,我得到:
<ul class="siteMap">
<li>
<a href="/">Home</a>
</li>
<li>
Administration
<ul>
<li>
<a href="/Home/Personnel">Personnel</a>
<a href="/Home/PersonnelNew">New Personnel</a>
</li>
</ul>
</li>
<li>
<a href="/Home/About">About</a>
</li>
</ul>
当我点击Holiday菜单项时,我得到:
<ul class="siteMap">
<li>
<a href="/">Home</a>
</li>
<li>
Administration
<ul>
<li>
<a href="/Home/Holiday">Holiday</a>
<a href="/Home/HolidayNew">New Holiday</a>
</li>
</ul>
</li>
<li>
<a href="/Home/About">About</a>
</li>
</ul>
这不是您想要的确切功能,但希望它足以让您入门。
<强>更新强>
<强>的Web.config 强>
<siteMap defaultProvider="MvcSiteMapProvider" enabled="true">
<providers>
<clear />
<add name="MvcSiteMapProvider"
type="MvcSiteMapProvider.DefaultSiteMapProvider, MvcSiteMapProvider"
siteMapFile="~/Mvc.Sitemap"
securityTrimmingEnabled="true"
cacheDuration="5"
enableLocalization="true"
scanAssembliesForSiteMapNodes="true"
includeAssembliesForScan=""
excludeAssembliesForScan=""
attributesToIgnore="clickable,visibility"
nodeKeyGenerator="MvcSiteMapProvider.DefaultNodeKeyGenerator, MvcSiteMapProvider"
controllerTypeResolver="MvcSiteMapProvider.DefaultControllerTypeResolver, MvcSiteMapProvider"
actionMethodParameterResolver="MvcSiteMapProvider.DefaultActionMethodParameterResolver, MvcSiteMapProvider"
aclModule="MvcSiteMapProvider.DefaultAclModule, MvcSiteMapProvider"
siteMapNodeUrlResolver="MvcSiteMapProvider.DefaultSiteMapNodeUrlResolver, MvcSiteMapProvider"
siteMapNodeVisibilityProvider="MvcSiteMapProvider.DefaultSiteMapNodeVisibilityProvider, MvcSiteMapProvider"
siteMapProviderEventHandler="MvcSiteMapProvider.DefaultSiteMapProviderEventHandler, MvcSiteMapProvider" />
</providers>
</siteMap>