0

I'm trying to implement a partial view but I'm having troubles with the model. I'm getting the following error:

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

The HTML code where the error is detected is the following:

@model IEnumerable<BUGTRACKER.Models.Revisiones>


<table class="table">
<tr>
    <th>
        @Html.DisplayNameFor(model => model.Version)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Descripción)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Fecha)
    </th>
    <th></th>
</tr>

@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Version)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Descripción)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Fecha)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
            @Html.ActionLink("Details", "Details", new { id = item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.Id })
        </td>
    </tr>
}

I really don't know what's going on here, any idea?

EDIT: This is my action

public ActionResult _Index()
{
    return PartialView(db.Revisiones.ToList());
}
Tieson T.
  • 20,774
  • 6
  • 77
  • 92
Aldridge1991
  • 1,326
  • 6
  • 24
  • 51
  • Didn't understand that. Could you please be more specific? – Aldridge1991 Jul 23 '15 at 15:14
  • Can you show the database code? There's not enough to go on from here – levelnis Jul 23 '15 at 15:39
  • db has some entries. I added the controller action – Aldridge1991 Jul 23 '15 at 15:45
  • Are any of the `Revisiones` objects null? – levelnis Jul 23 '15 at 15:47
  • It's very strange because if I write in the URL: http://localhost:52404/Revisiones/_Index I can see all the data. I want to show it in a partial view of the home index and there's where the error shows – Aldridge1991 Jul 23 '15 at 15:50
  • So I'd say the problem is related to the partial view – Aldridge1991 Jul 23 '15 at 15:51
  • Like @leveInis sad, Aren't there any item of Model null? Put an "if(item!=null)" inside the foreach. Just to check – Thiago Jul 23 '15 at 15:52
  • Are you accessing the partial via `Html.Partial` or `Html.Action` and how are you creating the db object within the controller? It sounds like the db is not instantiated correctly when it's accessed – levelnis Jul 23 '15 at 15:54
  • It does not enter the foreach method. – Aldridge1991 Jul 23 '15 at 15:54
  • private BugTrackerEntities2 db = new BugTrackerEntities2(); // GET: Revisiones public ActionResult _Index() { return PartialView(db.Revisiones.ToList()); } – Aldridge1991 Jul 23 '15 at 15:55
  • So probably your entire Model is null – Thiago Jul 23 '15 at 15:56
  • Couldn't you create a Model for the parent index page and add a property for the IEnumerable - then you can pass that property to the partial – levelnis Jul 23 '15 at 15:57
  • I think the problem is the Index action from the Revisiones Controller is not executed so when I try to set the index partial view, there's notthing to show. I'm trying to show db information in the index page. – Aldridge1991 Jul 23 '15 at 15:58
  • I didn't understand why you cant reach the action and the error is on partialview. – Thiago Jul 23 '15 at 16:02
  • I try to show some data in the INDEX of the site. So, when the site is loaded, it'll try get some data from the database but it's empty as it's populated in the action of the controller which hasn't been executed yet. – Aldridge1991 Jul 23 '15 at 16:03
  • 2
    Try using `Html.Action` instead of `Html.Partial` to render the partial. See here: http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx/ – levelnis Jul 23 '15 at 16:06
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) –  Jul 24 '15 at 01:37

3 Answers3

0

@levelnis approach did it!

Try using Html.Action instead of Html.Partial to render the partial.

This worked for me.

Aldridge1991
  • 1,326
  • 6
  • 24
  • 51
0

From the Viewmodel you are returning List<BugTracker.Models.Revisiones> List, in the view as well the type of Model is correct.

But you are trying to access the properties that are in the ViewModel and not in the List iterator.

Version, Description, Fecha are not part of BugTracker.Models.Revisiones class I suppose.

Solution would be to return the RevisonesViewModel from ViewModel


    public ActionResult _Index()
    {
        return PartialView(object of RevisonesViewModel);
    }

instead of


    public ActionResult _Index()
    {
        return PartialView(db.Revisiones.ToList());
    }

Ali Sufyan
  • 94
  • 1
  • 3
-2

Your Model is an IEnnumerable of BUGTRACKER.Models.Revisiones

When you iterate it's ok since it's a list.

However, you can not do this: model.Version since it doesNot exist in your model. What you need is a model that encapsulate your properties and a list of BUGTRACKER.Models.Revisiones.

Public Class RevisonesViewModel{
      public string Version{ get; set; }
      public string Fetch{ get; set; }
      public string Description{ get; set; }
      public IEnumerable<BUGTRACKER.Models.Revisiones> MyRevisiones
}
Oualid KTATA
  • 1,116
  • 10
  • 20
  • 1
    Erm, you can actually, as I just found out. See here: http://stackoverflow.com/questions/20807869/displaynamefor-from-listobject-in-model – levelnis Jul 23 '15 at 16:55