NullreferenceException when iterating over Model.Nodes

Apr 5, 2011 at 7:06 PM


I have some problem with  MvcSiteMap. I get NullreferenceException when interating over Model.Nodes because Model is NULL.

I got it working with @Html.MvcSiteMap().Menu() but when I try the samples from your DisplayTemplate I get that NullreferenceException.

I have copy, pasted the code from the template, so I cant see why it is not working. Maybe I have forgot something ?

Heres my code:


<?xml version="1.0" encoding="utf-8" ?>
<mvcSiteMap xmlns="" enableLocalization="false" >
  <mvcSiteMapNode title="TestProject" action="Index" controller="Home" changeFrequency="Always" updatePriority="Normal">
    <mvcSiteMapNode title="Test" action="Index" controller="Home">
      <mvcSiteMapNode title="Test1_5" action="Test1_5" controller="Home">
        <mvcSiteMapNode title="Test1_4" action="Test1_4" />
        <mvcSiteMapNode title="Test1_1" action="Test1_1" />
        <mvcSiteMapNode title="Test1_6" action="Test1_6" />
        <mvcSiteMapNode title="Test1_2" action="Test1_2"/>
        <mvcSiteMapNode title="Test1_3" action="Test1_3"/>


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

    ViewBag.Title = "TestSiteMap";


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


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication3.Controllers
    public class HomeController : Controller
        public ActionResult Index()
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            return View();

        public ActionResult TestSiteMap()
            return View();

I cant figure out what I am doing wrong!

Hope anyone can help! Thanks!