tags:

views:

81

answers:

1

This is a strange problem because yesterday I was executing my code in Cassini (VS2008) and in the action method of the controller the Session object was valid and holding session data. Today I restarted VS2008 and opened the project and the Session object is null.

What are the reasons that the Session object could be null in the Controller code in an ASP.NET MVC application?

A: 

I discovered the problem.

I was using LINQ to retrieve an IEnumerable from the business layer and then iterating over it in the view. Problem was that LINQ was lazy-loading and did not try and load the contents of the list (which is stored in a Session object) until it tries to iterate over it in the View. I solved this by using the .ToList() extension method to force a load in the Controller.

Guy