Search code examples
asp.net-mvc-4razor-2mvcsitemapprovider

Customizing output of MVCSiteMap for child menu of root node


I am trying to customize the look of the outputted layout for the MVCSiteMap and have been modifying the MenuHelperModel.cshtml with some success. The only problem I have is I can not figure out how to make how to make sub lists of the first SiteMapNode? Currently the output looks like:

•Home 
•Contact US 
•News
    Sports 
•About 

But what I want it to look like is:

•Home 
     Contact US 
•News
    Sports 
•About

I am unable to figure out how to make the Contact Us link a sub-componate of the Home link, like Sprots is for News.

Here is my SiteMap code:

<?xml version="1.0" encoding="utf-8" ?>
<mvcSiteMap xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-3.0"
            xsi:schemaLocation="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-3.0 WebSiteMapSchema.xsd"
            enableLocalization="true">

    <mvcSiteMapNode title="Home" controller="Home" action="Index">
        <mvcSiteMapNode title="Contact US" controller="Home" action="Contact" />

        <mvcSiteMapNode title="News" controller="Home" action="News">
            <mvcSiteMapNode title="Sports" controller="Home" action="Sprots" />
        </mvcSiteMapNode>

        <mvcSiteMapNode title="About" controller="Home" action="About"/>
    </mvcSiteMapNode>
</mvcSiteMap>

And the MenuHelperModel.cshtml:

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

<ul>
    @foreach (var node in Model.Nodes) { 
        <li>@Html.DisplayFor(m => node)
            @if (node.Children.Any()) {
                @Html.DisplayFor(m => node.Children)
            }
        </li>
    }
</ul>

What do I have to modify so that the Contact Us becomes a child of the Home link?


Solution

  • I think I figured it out, it looks like if I use this code to render the SiteMap it does not display the first node, this will allow me to build a new home menu and add child options. I have yet to try this on sub pages since I only have an index page built. Once I test it on sub pages to make sure it still displays correctly I will update.

    @Html.MvcSiteMap().Menu(false)