Reputation: 28853
I have created a partial view in my asp.net mvc solution that will show a list of items from a database. However I get an error saying: Object reference not set to an instance of an object.
I'm not sure why this is happening because as far as I'm concerned everything is fine. The list of items are venues which is controlled via VenuesController BUT the partial view is inside the /shared/ folder and NOT the /venues/ folder for the views so is asp.net complaining because it's not linking the two together?
This is the code I have in my VenuesController:
//
// GET: /Venues/
public ActionResult Index()
{
return View(_repository.ListAll());
}
public ActionResult VenuesList()
{
return PartialView("VenuesList",_repository.ListAll());
}
The Index() works fine and displays the data in /Views/Venues/Index.aspx but the VenuesList() throws the error :/
Thanks.
Upvotes: 0
Views: 741
Reputation: 5029
Make sure that any properties in your model that you reference in your partial view are not null. I've had similar cases where the model passed into the partial view has some null fields that, when rendered, throw NullReferenceExceptions.
E.g. something like
<%: Model.Property %>
or, especially:
<%: Model.Property.ChildProperty %>
i.e. if "Property" is null, then trying to access "ChildProperty" WILL throw a NulLReference exception.
Upvotes: 1
Reputation: 76597
The error message that you mention:
Object reference not set to an instance of an object.
typically deals when a property of a collection is referenced and it is null. You might want to make sure that the list of items is either:
Any code from the Controller-side of things might make this a bit easier to figure out.
Update:
I'm not completely sure what the Venue model looks like, however two things could be occurring here.
In the constructor for Venue (which should be passed the list from your repository), the List property inside of Venue is never being instantiated prior to Adding the items in.
or
The repository is not returning any values.
Additional code displaying the Venue class and it's constructor might be helpful.
Upvotes: 1