This example shows how to configure Telerik Menu for ASP.NET MVC to bind to a SiteMap.

The required steps are:

  1. Define a sitemap and add it to the ViewData. This example is using the SiteMapManager which allows you to create sitemaps. The PopulateSiteMap attribute adds a registered sitemap to the ViewData with a key specified by the ViewDataKey property:
    [PopulateSiteMap(SiteMapName = "sample", ViewDataKey = "sample")]
    [SourceCodeFile("Sitemap", "~/sample.sitemap")]
    public ActionResult SiteMapBinding()
    {
        if (!SiteMapManager.SiteMaps.ContainsKey("sample"))
        {
            SiteMapManager.SiteMaps.Register<XmlSiteMap>("sample", sitmap => sitmap.LoadFrom("~/sample.sitemap"));
        }
    
        return View();
    }
            
  2. Use one of the overloads of the BindTo method to bind the menu. The first overload requires only the sitemap's name. The second overload requires the sitemap name and an Action<MenuItem>, which you can use to set properties for each databound item.
    Both overloads will set the RouteName, ControllerName, ActionName, Url properties of the menu item, if they are set for the corresponding SiteMapNode object.
    <%= Html.Telerik().Menu()
           .Name("Menu")
           .BindTo("SiteMapName")
    %>
            
    or ...
    <%= Html.Telerik().Menu()
            .Name("Menu")
            .BindTo("SiteMapName", (item, siteMapNode) =>
            {
                item.Selected = siteMapNode.Action == "Index";
            })
    %>