【问题标题】:Object reference not set to an instance of an object in line of Layout in View对象引用未设置为视图中布局行中的对象实例
【发布时间】:2021-03-17 20:57:09
【问题描述】:

我会为这个错误而疯狂!

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

我的观点的一部分:

@{
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


    【解决方案1】:

    我意识到了问题所在!

    其中一个菜单中有一个 Session,它的值为 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多