0

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.

1
  • You will not get an object reference exception if the view could not be found. Have you set a break point in your controller action to see if there is any problem. When a view is not found, you will get the error like - The view 'Index' or its master was not found. The following locations were searched: ~/Views/Search/Index.aspx ~/Views/Search/Index.ascx ~/Views/Shared/Index.aspx ~/Views/Shared/Index.ascx Description: An unhandled exception occurred during the execution of the current web request. Commented Mar 24, 2011 at 19:16

2 Answers 2

1

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:

  • Not empty.
  • Instantiated properly.

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Which controller would a partial view use though if it were inside the /Views/Shared/ folder?
Are you passing a model to the Partial View? Whatever the model is that is populating your View and the action where the View is built would be helpful.
Yes I'm passing the model to the view like so in the header: <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<u0558234_assignment2.Models.Venue>>" %> BUT I'm not sure which controller I would be using as the View is inside the shared folder and NOT the venues folder so what controller should my action be in?
Maybe posting the Venue class and it's constructor to be helpful. You would typically have an Action in one of your controllers that will populate your Partial View - it would probably resemble this : public ActionResult [NameOfYourView] ([arguments]) and it would return something like : return PartialView([model]);
But which controller though? As the VenuesController controls the views here: /Views/Venues/ right? and wouldn't talk to the views inside the /Views/Shared/ folder or would it?
|
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.