Domanda

In my _Layout.cshtml, I store some session data in the ViewBag, like so:

if (Session["SiteSession"] != null)
{
    SiteSession siteSession = (SiteSession)Session["SiteSession"];
    ViewBag.SiteSession = siteSession;
}

and it's been working great so far.

Until now. I have a view that displays some data and opens up certain controls to Administrators based on ViewBag.SiteSession.IsAdmin, and for some reason that value is inaccessible (or null, according to intellisense):

RuntimeBinderException: Cannot perform runtime binding on null reference

Has anybody had this issue? Or perhaps knows where the problem is rooted? All help is appreciated.

È stato utile?

Soluzione

It turns out that I had to change the way ViewBag.SiteSession was set to begin with.

My colleague placed that code in _Layout.cshtml, when the code should have been in our BaseController and executed during OnAuthorization().

Altri suggerimenti

ViewBag is a dynamic wrapper of the ViewData object (which is just a Dictionary), which is used to communicate data between a controller and its view. To this end, its a fantastic tool and extremely useful. However, you cannot use ViewBag to pass data between multiple ActionMethods.

To accomplish this, you must use TempData, which is stored in the session.

A previous answer to a similar question can be found here.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top