Telerik Extensions for ASP.NET MVC

Version Q1 2012, released 04/19/2012

About this example ASPXRazor

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";
            })
    %>
            

Get more than expected!

Take the Telerik Extensions for ASP.NET MVC to your Visual Studio projects to truly experience their power. Download your free copy now and jumpstart your development with the available learning resources.

The Extensions are offered in both open source and commercial versions the differences between which are well explained in the licensing FAQ.

If you have any questions, do not hesitate to contact us at sales@telerik.com.

Other Demos: