[英]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.