简体   繁体   English

如何避免在View中提供空模型引用的异常

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

相关问题 在ASP.NET MVC视图模型上抑制来自Resharper的可能的空引用异常消息 - Suppressing Possible null reference exception message from Resharper on a asp.net mvc view model 当没有将模型类的实例传递给视图时,如何避免Create.cshtml中的EditorFor(x = x.Prop)生成的null异常? - How to avoid null exception generated by EditorFor(x=x.Prop) in Create.cshtml when no instance of model class is passed to the view? 如果model为null,如何处理null异常,如何在mvc中的视图中处理它? - how to handle null exception if model is null , how to handle it in view in mvc? 将图像从数据库传递到视图,空引用异常错误 - Pass Image from Database to View, Null Reference Exception Error 是否有一种简便的方法来检查空值,以避免空引用异常? - Is there a shorthand way to check for a null to avoid a null reference exception? 如果从数据库返回的字符串为null或为空,如何避免异常? - How to avoid exception in case if string returned from database is null or empty? 来自 APIGatewayProxyFunction 的空引用异常 - Null Reference Exception from APIGatewayProxyFunction 来自对象的空引用异常 - Null reference exception from object 当数据网格行在网格内不可见时,如何避免Null引用异常? - How to avoid Null reference exception when datagrid rows are not visible within grid? 这如何产生空引用异常? - How is this generating a null reference exception?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM