2013-12-10 104 views
0

我希望我能做到以下几点:@Html.MvcSiteMap().PageMenu()而不是@Html.MvcSiteMap().Menu("MenuBtnGroup")如何创建新模型?

主要是因为过滤器。

我希望某个环节只出现在页面上,或在主应用程序菜单

<mvcSiteMapNode title="Usuários" controller="Usuarios" action="Index"> 
    <mvcSiteMapNode title="Novo Usuário" action="Novo" visibility="SiteMapPathHelper,PAGEMENU-ONLY,!*" /> 
    <mvcSiteMapNode title="Detalhes" action="Detalhes" visibility="SiteMapPathHelper,!*" dynamicNodeProvider="UsuarioDynamicNodeProvider, Web"> 
     <mvcSiteMapNode title="Editar" action="Editar" inheritedRouteParameters="id" /> 
    </mvcSiteMapNode> 
</mvcSiteMapNode> 

回答

1

这个功能不是内置(还)网站地图,并没有,但有一个办法通过构建自己的可见性提供者并使用SourceMetaData将菜单的名称传递到可见性逻辑来实现。

/// <summary> 
/// Filtered SiteMapNode Visibility Provider for use with named controls. 
/// 
/// Rules are parsed left-to-right, first match wins. Asterisk can be used to match any control or any control name. Exclamation mark can be used to negate a match. 
/// </summary> 
public class CustomFilteredSiteMapNodeVisibilityProvider 
    : SiteMapNodeVisibilityProviderBase 
{ 
    #region ISiteMapNodeVisibilityProvider Members 

    /// <summary> 
    /// Determines whether the node is visible. 
    /// </summary> 
    /// <param name="node">The node.</param> 
    /// <param name="sourceMetadata">The source metadata.</param> 
    /// <returns> 
    ///  <c>true</c> if the specified node is visible; otherwise, <c>false</c>. 
    /// </returns> 
    public override bool IsVisible(ISiteMapNode node, IDictionary<string, object> sourceMetadata) 
    { 
     // Is a visibility attribute specified? 
     string visibility = string.Empty; 
     if (node.Attributes.ContainsKey("visibility")) 
     { 
      visibility = node.Attributes["visibility"].GetType().Equals(typeof(string)) ? node.Attributes["visibility"].ToString() : string.Empty; 
     } 
     if (string.IsNullOrEmpty(visibility)) 
     { 
      return true; 
     } 
     visibility = visibility.Trim(); 

     // Check for the source HtmlHelper 
     if (sourceMetadata["HtmlHelper"] == null) 
     { 
      return true; 
     } 
     string htmlHelper = sourceMetadata["HtmlHelper"].ToString(); 
     htmlHelper = htmlHelper.Substring(htmlHelper.LastIndexOf(".") + 1); 

     string name = sourceMetadata["name"].ToString(); 

     // All set. Now parse the visibility variable. 
     foreach (string visibilityKeyword in visibility.Split(new[] { ',', ';' })) 
     { 
      if (visibilityKeyword == htmlHelper || visibilityKeyword == name || visibilityKeyword == "*") 
      { 
       return true; 
      } 
      else if (visibilityKeyword == "!" + htmlHelper || visibilityKeyword == "!" + name || visibilityKeyword == "!*") 
      { 
       return false; 
      } 
     } 

     // Still nothing? Then it's OK! 
     return true; 
    } 

    #endregion 
} 

然后,你可以给他们一个“名” SourceMetadata属性名称每个菜单。

@Html.MvcSiteMap().Menu(new { name = "MainMenu" }) 
@Html.MvcSiteMap().Menu(new { name = "PageMenu" }) 

然后在您的配置中使用CustomFilteredSiteMapVisibilityProvider而不是FilteredVisibilityProvider。完整示例请参见this answer