This is a migrated thread and some comments may be shown as answers.
MVCSitemapProvider
8 Answers 240 Views
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Simon
Top achievements
Rank 1
Simon asked on 25 Oct 2010, 01:41 AM
Hi All,

This is my first post so go easy on me :)

I'm trying to integrate Telerik Menus into my MVC2 application.

I'm using:

VS2010 Ultimate
ASP.Net MVC 2 (.Net 4 in VB)
Telerik MVC DLL Ver 2010.2.825.235

My site uses the MVCSitemapProvider available here: http://mvcsitemap.codeplex.com/

MVCSitemapProvider exposes itself as a standard sitemap provider to ASP.Net MVC and also as an "MVCSitemap" through an MVCSitemapProvider namespace. It reads .mvcsitemap files (example below)

<?xml version="1.0" encoding="utf-8" ?>
<mvcSiteMap xmlns="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-2.0" enableLocalization="true">
    <mvcSiteMapNode title="Root" controller="Root" action="Root" changeFrequency="Always" updatePriority="Normal">
        <mvcSiteMapNode title="Login" controller="Public" action="Login" />
        <mvcSiteMapNode title="Admin" controller="Admin" action="Index">
            <mvcSiteMapNode title="Statistics" controller="Admin" action="Statistics" />
        </mvcSiteMapNode>
        <mvcSiteMapNode title="Home" controller="Home" action="" />
    </mvcSiteMapNode>
</mvcSiteMap>

I need to load the sitemap output from  MVCSitemapProvider into a Telerik menu and I just don't seem to be able to work out how I should do it.

I've tried: recursively iterating through the sitemap and building Telerik UI MenuItems into the appropriate structure (this works fine) but I don't know how to bind that structure to the menu on the page (and the only demo I can find was for ASP.Net assuming a menu control which could be accessed from a code-behind?)

I've also tried using the

        SiteMapManager.SiteMaps.Register(Of XmlSiteMap)("examples", Function(sitemap) sitemap.LoadFrom("/Web.sitemap"))

syntax in Global.asax but I can't work out how to cast the sitemap into the appropriate type (and I definitely don't want to load from file as I don't have a "normal" sitemap)

Can someone please tell me how to bind this? If you need more information, just let me know

Many thanks

8 Answers, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 25 Oct 2010, 08:02 AM
Hello Simon,

If you have create a collection of MenuItem object, then you just need to bind the menu to them. Check this help topic which shows how to do this.

Kind regards,
Georgi Krustev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Jann
Top achievements
Rank 1
answered on 19 Jan 2011, 09:16 AM
Html.Telerik().Menu()
    .Name("Menu")
    .BindTo(Html.MvcSiteMap().Provider.RootNode.ChildNodes,
        mappings => mappings.For<MvcSiteMapNode>(binding => binding
            .ItemDataBound((item, node) =>
                {
                    item.Text = node.Title;
                    item.ActionName = node.Action;
                    item.ControllerName = node.Controller;
                })
                .Children(node => node.ChildNodes)))
    .Render();

I use this piece of code to bind to my MvcSiteMap.
0
Simon
Top achievements
Rank 1
answered on 19 Jan 2011, 11:30 AM
Thank you both for your responses - especially the code snippet from Jann, it was very helpful and did solve my problem

Cheers
0
Jesse
Top achievements
Rank 1
answered on 06 Mar 2011, 09:50 PM
I am in an MVC 3 Razor environment and Jann's code does not work for me. What using statements if any are you including in your view?

After fighting with it for awhile I finally got the "are you missing an assembly reference?" errors to go away but then got a null reference error.

Are there any additional assumptions I should be aware of in order to get this code to work. I too am trying to get the MVC Sitemap provider to work with telerik menu.
0
Jann
Top achievements
Rank 1
answered on 07 Mar 2011, 09:29 AM
Hi the only update I had to make to the code above is to repplace the assignments to item.ControlelrName and item.ActionName into
item.Url = Url.Action(node.Action, node.Controller, new {node.Area});

 I use the following include besides the Telerik include:

<%

@ Import Namespace="MvcSiteMapProvider" %>

 

0
darenkov
Top achievements
Rank 1
answered on 14 Mar 2011, 09:18 AM
i have noticed one problem with this approach. when my site hierarchy is deeper than 2 levels the menu will only show the path containing the controller and action, yet i want it to display the full path such as all 3 items in the nested hierarchy. for example based on the following sitemap when i hover over the menu item it shows:

about-us/john-smith  instead of about-us/team/john-smith

<mvcSiteMapNode title="About Us" controller="AboutUs" action="Index">
 
<mvcSiteMapNode title="Team" action="team">
   
<mvcSiteMapNode title="John Smith" action="john-smith">
  </mvcSiteMapNode>
</mvcSiteMapNode>
0
darenkov
Top achievements
Rank 1
answered on 16 Mar 2011, 10:16 AM
that is fine but what if you have hierarchy with 3 levels or more and want to show the 3 levels in the path selection, ie

one/two/three
0
Sid
Top achievements
Rank 1
answered on 20 Apr 2011, 12:16 AM
Hi,

I've also used Jann's example and am able to build/publish without error.  However, when I view the site in browser, an error comes up on the line with the Render command:
@using Telerik.Web.Mvc.UI;
@using MvcSiteMapProvider;
  
@(Html.Telerik().Menu()
    .Name("Menu")
        .BindTo(Html.MvcSiteMap().Provider.RootNode.ChildNodes,
            mappings => mappings.For<MvcSiteMapNode>(binding => binding
                .ItemDataBound((item, node) =>
                {
                    item.Text = node.Title;
                    item.ActionName = node.Action;
                    item.ControllerName = node.Controller;
                })
                    .Children(node => node.ChildNodes)))
        .Render();
  
)

Can anyone suggest how I'd rectify the error message  CS1026: ) expected

Thanks,
Sid
Tags
Menu
Asked by
Simon
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
Jann
Top achievements
Rank 1
Simon
Top achievements
Rank 1
Jesse
Top achievements
Rank 1
darenkov
Top achievements
Rank 1
Sid
Top achievements
Rank 1
Share this question
or