MVC3 asp.net 4.0 Entity Framework 4.02 SQLCE 4 and SEO part 2 MVCSitemapProvider

I am currently playing around with the MVC Sitemap provider for my new website bankrutt.se and have discovered the easiest way to create a top menu in MVC3.

The top menu shipped in the default MVC3 template don’t have support for highlighting the selected node in the menu.

This is a must and the easiest way to achieve this is to do the following:

Download the MVC Sitemap provider using GitHub in Visual studio, its important to do this with GitHub because it will install a lot of the configuration and a couple of razor/ascx helper methods.

Edit the Mvc.sitemap file so it corresponds to your web site.

Find the <ul id=menu> </ul> in the _layout.cshtml and replace it with

   @Html.MvcSiteMap().Menu() 

If you would like to have a bread crumb trail you could easily achive this by adding  

<div id="breadCrumbs">@Html.MvcSiteMap().SiteMapPath()</div>

Now to the selected part
You have got a lot of helper files installed under Shared/Display Templates (you can delete all the .ascx if you are running razor)
Open up MenuHelperModel.cshtml
Alter it to look like this

@model MvcSiteMapProvider.Web.Html.Models.MenuHelperModel
@using System.Web.Mvc.Html
@using MvcSiteMapProvider.Web.Html.Models

<ul id="menu">
    @foreach (var node in Model.Nodes) { 

          if (node.IsCurrentNode) { 
             <li class="selected">@Html.DisplayFor(m => node) </li>
             }

        else{
      <li>
        @Html.DisplayFor(m => node) 
            @if (node.Children.Any())
            {
                @Html.DisplayFor(m => node.Children)
            }
        </li>
        }
    }
</ul>

Now your good to go!
You got a nice top menu and a nice bread crumb trail with no effort at all!
Remeber you MUST use GitHub pm Install-Package MvcSiteMapProvider in Visual studio to get this working.

2 thoughts on “MVC3 asp.net 4.0 Entity Framework 4.02 SQLCE 4 and SEO part 2 MVCSitemapProvider

  1. Had just started using this and was looking for something else for my left navbar menu and ran across this here. Saved me having to do much other than copy/paste!

    Another ToDo crossed off my list!

  2. I tried this and I get an error, not sure where to go from here :

    Constructor on type ‘System.Web.Mvc.WebFormView’ not found.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.MissingMethodException: Constructor on type ‘System.Web.Mvc.WebFormView’ not found.

    Source Error:

    Line 23:
    Line 24:
    Line 25: @Html.MvcSiteMap().Menu()
    Line 26:
    Line 27:

Leave a comment