【问题标题】:I am getting Error in Asp.Net MVC "Cannot perform runtime binding on a null reference"我在 Asp.Net MVC 中遇到错误“无法对空引用执行运行时绑定”
【发布时间】:2022-07-20 16:24:40
【问题描述】:

该项目正常运行,无一例外,然后我突然开始收到此错误。我在论坛上搜索了这个错误并想出了一些关于 Razor 的信息,所以我更新了 Visual Studio,但没有任何改变。详情如下:

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>

控制器:

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();
    }

我的错误: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:无法对空引用执行运行时绑定

【问题讨论】:

  • 这个错误显然告诉你你绑定了一个值为空的对象。如我所见,我猜它发生在withEmailToUser.UserWrongLoginLogs = userWrongLoginLogs 行中,这是因为您的withEmailToUser 对象为空
  • 不,控制器部分没有空值。
  • 删除 @ViewBag.countActivityFinish 会在其上方的 viewbag 中产生错误。新页面上的底部视图包给出了这个错误
  • 请在控制器中尝试ViewBag.countActivityFinish 的默认值,我认为它可以解决您的异常;示例:ViewBag.countActivityFinish= countActivityFinish??0
  • 当我按值按照步骤进行时,听起来我想要。但是由于某种原因,最新的 viewbag 总是抛出错误

标签: c# asp.net asp.net-mvc


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    相关资源
    最近更新 更多