I've created a PartialView and here's how I'm calling it.
<div id="bodyarea">
<div id="leftnavigationbar">
@Html.Partial("_SideBarMenu")
</div>
<div id="mainbody">
@RenderBody()
</div>
<div id="footer">
</div>
</div>
Here's the actual code for the PartialView:
@model Cumavi.ViewModels.SidebarNavigation
<ul>
@foreach (var category in Model.Categories)
{
<li>category.Name</li>
}
</ul>
As you can see, I'm using a custom created ViewModel called SidebarNavigation, which has this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Cumavi.Models;
namespace Cumavi.ViewModels
{
public class SidebarNavigation
{
public IEnumerable<Category> Categories { get; private set; }
public SidebarNavigation()
{
CategoryRepository categoryRepo = new CategoryRepository();
this.Categories = categoryRepo.FindAllCategories();
}
}
}
The problem is that when I run the application, I get a null reference exception on the foreach loop.
I don't understand the reason though. In the ViewModel, SidebarNavigation, in the constructor I'm actually filling the variable. Any suggestions?
Edit:
Another thing I've noticed is that the constructor for my ViewModel class is never actually called. :S That must be why the Categories attribute is null. Suggestions?
Edit 2:
Another problem! I'm using _Layout.cshtml file to create the common look (masterpage) for the application. Since no controller is associated to this file, how can I pass a model to it? :S