简体   繁体   中英

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.

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. 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. Probably intellisense shows error exact line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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