[英]How to avoid from exception on providing null model reference in View
In this Model "HomePageModel" contains three sub-models named as "JoinedClasses","RequestedClasses" and "tbl_Student". 在该模型中,“ HomePageModel”包含三个名为“ JoinedClasses”,“ RequestedClasses”和“ tbl_Student”的子模型。 If a user still didn't have joined a class then its object passed should be null as shown in fig. 如果用户仍未加入某个类,则其传递的对象应为null,如图5所示。 and I have checked through if-condition to avoid from exception but it always throws "Object reference not set to an instance of an object.", how to avoid from this exception on null reference provision 并且我已经检查了if-condition来避免出现异常,但是它总是抛出“对象引用未设置为对象的实例。”,如何避免在提供空引用时出现此异常
.Equals()
cannot be used to compare with null
values, because if an object is null, it won't have the method .Equals()
! .Equals()
不能用于与null
值进行比较,因为如果对象为null,则它将没有.Equals()
方法! In your case, the method itself is throwing the Nullreference exception. 在您的情况下,方法本身将引发Nullreference异常。
Just change to: 只需更改为:
@if(Model.JoinedClasses != null) {
//...code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.