简体   繁体   English

ASP.Net Razor Entity Framework Null Reference Exception 使用 Ajax 时

[英]ASP.Net Razor Entity Framework Null Reference Exception when use Ajax

I am using ASP.NET MVC 5 + Entity Framework + Oracle Databace 11g.我正在使用 ASP.NET MVC 5 + 实体框架 + Oracle Databace 11g。

I am get NullReferenceException , when trying to use foreach .尝试使用foreach时出现NullReferenceException

@if (Model != null && Model.P_DCOBJ != null)
{
    foreach (var dcobj in Model.P_DCOBJ.Where(p => p != null)) //NRE Here
    ...
}

P_DCOBJ object of type ICollection (One to Many reference) ICollection 类型的 P_DCOBJ 对象(一对多引用)

However, this code works fine if I just refreshes the page, the error occurs when I try to add a new account using Ajax, a record is entered into the database, but when I try to display PartialView I get an exception.但是,如果我只是刷新页面,此代码就可以正常工作,当我尝试使用 Ajax 添加新帐户时会发生错误,将记录输入到数据库中,但是当我尝试显示 PartialView 时出现异常。

The error occurs in any attempt to access the object P_DCOBJ such as .Count.任何尝试访问对象 P_DCOBJ(例如 .Count)时都会发生错误。

I found a solution to this problem.我找到了解决这个问题的方法。 The error occurs in another place, but for whatever reason, the debugger shows it in foreach, and did not go in this cycle, although the problem object has been located below the cycle.错误发生在另一个地方,但是不管什么原因,调试器在foreach中显示出来,并没有进入这个循环,虽然问题对象已经定位在循环下面。 The facility was related entity relationship existed, but the object is equal to Null.设施是相关实体关系存在,但对象等于Null。

SOLUTION:解决方案:

If you experience this problem, check all the objects on the current page, or PartialView is equal to NULL .如果遇到此问题,请检查当前页面上的所有对象,否则PartialView等于NULL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET Razor实体框架Null参考异常 - ASP.NET Razor Entity Framework Null reference exception 在 ASP.NET 核心 Razor 页面中填充下拉列表时获取 null 引用异常 - Getting null reference exception when populating dropdown list in ASP.NET Core Razor Pages 在asp.net核心(Entity Framework)中播种和初始化数据库时,如何解决“空异常” - How to fix “Null exception” when seeding and initialising the database in asp.net core (Entity Framework) 单击ASP.NET Webforms中的按钮时,空引用异常 - Null Reference Exception when Clicking a button in ASP.NET Webforms ASP.NET MVC 5中的Null引用异常 - Null Reference exception in ASP.NET MVC 5 如何在 ASP.NET Core Razor Pages 项目的 _layout.cshtml 文件中使用 Razor 页面使用实体框架作为部分视图? - How to use Razor Page Using Entity Framework as a partial view in _layout.cshtml file in ASP.NET Core Razor Pages project? 实体框架Null参考异常 - Entity Framework Null Reference Exception 实体框架-空引用异常 - Entity Framework - Null Reference Exception 添加到桥表时,Entity Framework Null参考异常 - Entity Framework Null reference exception when adding to bridge tables 实体框架/ ASP.NET MVC 5 DateTime异常 - Entity Framework/ASP.NET MVC 5 DateTime Exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM