Ab3
Ab3

Reputation: 43

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?

Upvotes: 4

Views: 17812

Answers (2)

ddagsan
ddagsan

Reputation: 1826

If there is no error on cshtml page, close and reopen again. Probably intellisense shows error exact line.

Upvotes: 0

Markus
Markus

Reputation: 22436

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>
}

Upvotes: 9

Related Questions