4

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?

1
  • first of all, remove @ from Model and then make sure Model is not null Commented Jun 24, 2015 at 9:35

2 Answers 2

9

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>
}
Sign up to request clarification or add additional context in comments.

1 Comment

THANK YOU SO MUCH THIS WORKED!! i shall accept the answer when it lets me! I appreciate it very much
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.