为什么我不能检查View中的ViewModel属性是否== null?

mke*_*ell 0 c# asp.net-mvc html5 asp.net-mvc-4

简单的问题(Ihope),我想在视图中使用if语句检查我的ViewModel中的属性是否为null

@if(Model.AdminPractice.TimeNotes == null)
Run Code Online (Sandbox Code Playgroud)

这个原因是错误的,

你调用的对象是空的.

但是如果我不调用属性本身而只调用类,则if语句有效.

@if(Model.AdminPractice == null)
Run Code Online (Sandbox Code Playgroud)

这是因为Viewmodel是类的组合而不是属性,或者我应该在控制器中处理这个如果是这样的话?谢谢

查看型号......

public class AdminPracticeViewModel
{
    public Practice Practice { get; set; }

    public AdminPractice AdminPractice { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

It'*_*ie. 5

不,这是因为Model.AdminPractice null.你需要做的是:

@if(Model.AdminPractice == null || Model.AdminPractice.TimeNotes == null)
Run Code Online (Sandbox Code Playgroud)