对象引用未设置为视图中布局行中的对象实例

Object reference not set to an instance of an object in line of Layout in View

提问人:zaf y 提问时间:12/6/2020 更新时间:12/6/2020 访问量:115

问:

我会为这个错误而疯狂!

我有一个非常简单的视图,没有任何模型或...,并且在定义布局的行中收到空错误,而在其他页面中布局工作正常,有什么想法吗?

我的观点的一部分:

@{
ViewBag.Title = "رزرو";
Layout = "~/Views/Shared/_Layout.cshtml";   //Error is here
}

和控制器:

     public ActionResult Reserves()
     {
        ViewBag.reservelist = "";
        try
        {
            ViewBag.UnreadMessages = UnreadMessages(Session["PersonId"].ToString());
            Guid DoctorId = Guid.Parse(Session["PersonId"].ToString());
           
            List<Reserve> reservelist = db.Reserves.Where(r => r.DoctorId == DoctorId && r.Accepted 
         == null).ToList();
            ViewBag.reservelist = reservelist;
           
        }
        catch
        {

        }
        return View();
       
    }
asp.net asp.net-mvc asp.net-mvc-4 asp.net-web-api nullreferenceexception

评论


答:

0赞 zaf y 12/6/2020 #1

我意识到问题出在哪里!

其中一个菜单中有一个会话,其值为 null,愚蠢的 VS 找不到问题,

    <li>
         //The below session was null

       <a href="@Url.Action("Chat","Home", new { PId = Session["PId"].ToString() })">
        <i class="fas fa-comments"></i>
          <span>پیام‌ها</span>
         <small class="unread-msg">@ViewBag.UnreadMessages</small>
       </a>
      </li>