This is a matter of style and design, not syntax.
I have domain models that have several (due to the lack of a better term) navigation properties. Therefore, in my strongly typed parts view Foo, which has a type property Bar, I could do the following:
<%: Foo.Bar.Name %>
However, sometimes Bar is Null, so I get something like:
<%: Foo.Bar == null ? String.Empty : Foo.Bar.Name %>
In other cases, since the convenience of navigational properties, I could make even more chaining. The drawback, however, is the introduction of a more null check in my view.
Alternatively, I could do all the null checking in the ViewModel so that I go through something βcleanβ in the view. I am looking for some common sense ideas or recommendations to avoid over-checking the null in my views.
PS I am using ASP.NET MVC, but I think this question may be related to other MVC frameworks.
source
share