I get a "NullReferenceException was unhandled by user code" error with the following code in my View when I pass in a null value via my controller. There are situations where I want to pass in a null value, but I do not want an error thrown when this happens. What should I change my code to?
Originally my code was:
@foreach (var item in Model.MyModelStuff)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Bla.Title)
</td>
<tr>
}
I have tried the following with no success:
@foreach (var item in Model.MyModelStuff.Where( item => item.MyModelStuff != null))
etc. . .
How do I change the code so that it will handle null without throwing an error? I've read I may need to be returning an empty collection of my model (?), how would I go about doing that - if it is indeed the necessary thing to do?