Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot perform runtime binding on a null reference error

Tags:

c#

asp.net-mvc

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?

like image 491
Ab3 Avatar asked Jan 08 '23 02:01

Ab3


1 Answers

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>
}
like image 191
Markus Avatar answered Jan 29 '23 11:01

Markus