0

在我的_Layout.cshtml中,我将一些会话数据存储在 中ViewBag,如下所示:

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

到目前为止,它一直运行良好。

到现在。我有一个视图,它基于 显示一些数据并向管理员打开某些控件ViewBag.SiteSession.IsAdmin,并且由于某种原因该值无法访问(或者null,根据智能感知):

RuntimeBinderException: Cannot perform runtime binding on null reference

有人遇到过这个问题吗?或者也许知道问题的根源在哪里?感谢所有帮助。

4

2 回答 2

0

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().

于 2013-07-02T19:25:13.957 回答
0

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.

于 2013-07-02T19:25:31.187 回答