Unable to bind run time with null reference error

In my project, I have a partial view with the following code block that fulfills the following conditions:

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

Just as easy. When I run my project, the null model returns. I get the following error:

Cannot perform runtime binding on a null reference

I thought I already defined this in the instructions if.

Is there something I am missing?

+4
source share
2 answers

In your code, you only check the property FirstNamefor null or empty values, but not for the model itself. You also need to add validation for the model:

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

If there is no error on the cshtml page, close and close again. Intellisense probably shows the exact line of error.

0
source

All Articles