2012-03-05 22 views

答えて

5

2時間後、4時間、それが行くここ合計:。。

public static class MyHelpers 
{ 
    private static Dictionary<string, object> SourceMetadata = new Dictionary<string, object> { { "HtmlHelper", typeof(MenuHelper).FullName } }; 

    public static MvcSiteMapNode GetNodeByKey(this MvcSiteMapHtmlHelper helper, string nodeKey) 
    { 
     SiteMapNode node = helper.Provider.FindSiteMapNodeFromKey(nodeKey); 

     var mvcNode = node as MvcSiteMapNode; 

     return mvcNode; 
    } 
} 

は、今あなたがする必要があるすべてはHtml.MvcSiteMap @()の呼び出しGetNodeByKey( "にmykey")されているURL

ありません他のすべてのプロパティ(title、ImageUrl、targetFrame ..)が利用可能で、urlとtitleを使用して完全なアンカーリンクを作成するヘルパーも作成できます。

更新日:

public static MvcHtmlString MapLink(this MvcSiteMapHtmlHelper htmlHelper, string nodeKey) 
    { 
     return htmlHelper.MapLink(nodeKey, null, null); 
    } 
    public static MvcHtmlString MapLink(this MvcSiteMapHtmlHelper htmlHelper, string nodeKey, string linkText) 
    { 
     return htmlHelper.MapLink(nodeKey, linkText, null); 
    } 
    public static MvcHtmlString MapLink(this MvcSiteMapHtmlHelper htmlHelper, string nodeKey, string linkText, object htmlAttributes) 
    { 
     MvcSiteMapNode myNode = GetNodeByKey(htmlHelper, nodeKey); 
     //we build the a tag 
     TagBuilder builder = new TagBuilder("a"); 
     // Add attributes 
     builder.MergeAttribute("href", myNode.Url); 
     if (htmlAttributes != null) 
     { 
      builder.MergeAttributes(new RouteValueDictionary(htmlAttributes), true); 
     } 
     if (!string.IsNullOrWhiteSpace(linkText)) 
     { 
      builder.InnerHtml = linkText; 
     } 
     else 
     { 
      builder.InnerHtml = myNode.Title; 
     } 
     string link = builder.ToString(); 
     return MvcHtmlString.Create(link); 
    } 
:あなたは不思議に思った場合は、ここではリンクヘルパーのためのコードを行きます
関連する問題