简体   繁体   English

无法对空引用错误执行运行时绑定

[英]Cannot perform runtime binding on a null reference error

In my project, I have got a partial view with the following block of code doing some conditions like this: 在我的项目中,我有一个部分视图,下面的代码块执行这样的条件:

@if (!string.IsNullOrEmpty(Model.FirstName)) {
    <h3>  @Model.FirtsName </h3>
}

Just as simple as that. 就这么简单。 When I run my project, a null model is returned. 当我运行我的项目时,返回一个空模型。 I get the following error: 我收到以下错误:

Cannot perform runtime binding on a null reference

I thought I had already defined this in my if statement. 我以为我已经在if语句中定义了这个。

Is there anything that I am missing? 有什么我想念的吗?

In your code, you only check the FirstName property for null or empty values, but not the model itself. 在您的代码中,您只检查FirstName属性是否为null或空值,而不是模型本身。 You need to add a check for the model also: 您还需要为模型添加检查:

@if (Model != null && !string.IsNullOrEmpty(Model.FirstName)){
    <h3>  @Model.FirstName </h3>
}

If there is no error on cshtml page, close and reopen again. 如果cshtml页面上没有错误,请关闭并重新打开。 Probably intellisense shows error exact line. 可能intellisense显示错误的确切线。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM