Retrieving Items from a List in a Model

Let’s look at how you would get the List<Book> Books from the Author model. We’ll use Author’s Detail View and then extract that to a Partial View. A common error is to get a null pointer exception here:

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

This is fixed by adding Virtual to the Books property. Now, when we go to the Author’s Detail page, the Books property is instantiated and the list can be displayed.

List Books