This is a question about style and design rather than syntax.
I have domain models that have several (for lack of a better term) navigation properties. So in my strongly typed details view of Foo
which has a property of type Bar
I could do the following:
<%: Foo.Bar.Name %>
However, sometimes Bar is Null, so I end up with something like:
<%: Foo.Bar == null ? String.Empty : Foo.Bar.Name %>
In other cases, because the convenience of the navigation properties I could do even more chaining. The downside, however, is the introduction of more null checking in my View.
As an alternative, I could do all the null checking in a ViewModel so that I've passed off something "clean" to the View. I'm looking for some ideas or common sense guidelines for avoiding excessive null checking in my views.
P.S. I'm using ASP.NET MVC, but I think this question may be relevant to other MVC frameworks.