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 you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With