0

I am getting an error Object reference not set to an instance of an object. in running my MVC Html Razor. I search on google on how to create an IF-Else in razor and found some good answer. But in my case I am getting an error.

My Code

<td  style="text-align: left" >
                    <div id="divEBSN">
                    @if (Model.RCEducationalBackground.School.SchooldName != null)
                    {
                       <span style="color: Red">@Html.DisplayFor(model => model.RCEducationalBackground.School.SchooldName)</span>    
                    }
                    else { 
                        <span style ="color:Red">@Html.DisplayFor(model => model.RCEducationalBackground.SchoolNameEducBG)</span>
                    }
                    </div>
                </td>
  • Could you please provide some more detail on this? as in your `model` and way you are passing your `model` from `controller` to `view` – Guruprasad J Rao Nov 07 '16 at 07:51
  • 1
    Because either `RCEducationalBackground` or `School` is `null` –  Nov 07 '16 at 07:52
  • `SchooldName ` is not the object itself, its the property of your object. You are getting the error because School might be null or the RCEducationalBackground is null – DarkMakukudo Nov 07 '16 at 07:53
  • @StephenMuecke yes my If statement is null that's why I create an if statement. – Trafalgar D Law Nov 07 '16 at 07:53
  • `@if (Model.RCEducationalBackground.School != null)` – DarkMakukudo Nov 07 '16 at 07:54
  • @MidTwo I will try it. But I only want the SchoolName – Trafalgar D Law Nov 07 '16 at 07:55
  • If `Model` is `null` it wil throw the exception because you cannot get the `RCEducationalBackground` of `null`. If `RCEducationalBackground` is `null` you get the exception because you cannot get the `School` of `null` - ditto for the `SchooldName` property. You need to check all of them –  Nov 07 '16 at 07:55

0 Answers0