I am looking for a clean way to handle references to null objects in the LINQ to SQL model class when they are passed to the view.
A simple example.
Table A has an FK in TableB. An FK association may or may not exist for any row in table A.
My LINQ to SQL classes express this relation as ClassA.ClassB.Property, but in some cases ClassA.ClassB is a null object due to a null foreign key
I want to list ClassA.Property and ClassA.ClassB.Property in a table in a view
So far my code looks like
<td> <% if ((classA.classB) != null) { %> <%= Html.Encode(classA.classB.Property)%> <% } %> </td>
Is there a cleaner way to do this in a view?
I tried
<td> <%= Html.Encode(classA.classB.Property ?? "")%> </td>
But this will not work, since a null value is not a "Property", but a "class".
I'm new to all MVC work, but the view seems to be the right place to make a choice on how to handle the display of null values.
source share