@Html.DisplayNameFor for details model

I found there was no need for helper methods, extra code or looping through the collection. I just used the following:

@Html.DisplayNameFor(model => model.Carriers.FirstOrDefault().Name)

This still works even if FirstOrDefault() would return null because it’s only looking for meta data, the Name property itself is not accessed.

Many thanks to @Kurian who inspired this answer.

Leave a Comment