简体   繁体   中英

I am getting Error in Asp.Net MVC "Cannot perform runtime binding on a null reference"

The project was working correctly with no exception, then i started to get this error suddenly. I searched this error on forums and figured something about Razor so i updated Visual Studio but nothing changed. Details as below:

cshtml:

               <div class="small-box bg-success">
                    <div class="inner">

                        <h3> @ViewBag.countActivityFinish </h3>

                        <p>Tamamlanan Aktivite Sayısı</p>
                    </div>
                    <div class="icon">
                        <i class="ion ion-pie-graph"></i>
                    </div>
                    <a href="/Activity" class="small-box-footer">Aktiviteleri Gör <i class="fas fa-arrow-circle-right"></i></a>
                </div>

controller:

public ActionResult Index()
    {
        if (Session["user"] == null) return RedirectToAction("Index", "Login");

        conn.Open();

        User withEmailToUser = conn.Query<User>("SELECT * FROM [User] WHERE Email = @Email", new User() { Email = Session["user"].ToString() }).FirstOrDefault();

        List<UserWrongLoginLog> userWrongLoginLogs = conn.Query<UserWrongLoginLog>("SELECT * FROM [UserWrongLoginLog] WHERE UserId = @UserId", new UserWrongLoginLog() { UserId = (Guid)withEmailToUser.Id }).ToList();    
        foreach (var item in userWrongLoginLogs)
        {
            conn.Execute("DELETE FROM [UserWrongLoginLog] WHERE Id=@Id", item);
        }

        int countCompany = conn.Query<int>("SELECT COUNT(*) FROM Company WHERE IsDelete = @IsDelete", new Company() { IsDelete = false }).FirstOrDefault();
        int countContact = conn.Query<int>("SELECT COUNT(*) FROM Contact WHERE IsDelete = @IsDelete", new Contact() { IsDelete = false }).FirstOrDefault();
        int countActivityWaiting = conn.Query<int>("SELECT COUNT(*) FROM Activity WHERE Status = @Status", new Activity() { Status = 0 }).FirstOrDefault();
        int countActivityFinish = conn.Query<int>("SELECT COUNT(*) FROM Activity WHERE Status != @Status", new Activity() { Status = 0 }).FirstOrDefault();

        conn.Close();

        withEmailToUser.UserWrongLoginLogs = userWrongLoginLogs;

        ViewBag.countCompany = countCompany;
        ViewBag.countContact = countContact;
        ViewBag.countActivityWaiting = countActivityWaiting;
        ViewBag.countActivityFinish = countActivityFinish;
        ViewBag.user = withEmailToUser;
        return View();
    }

my error: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot perform runtime binding on a null reference

删除并重新创建我的视图页面后,粘贴相同的代码解决了我的问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM