0

I have an MVC 5 project with the next two classes as model:

public class Verbruik
    {
        public string Gemiddelde1 { get; set; }
        public string Gemiddelde2 { get; set; }
        public List<VerbruikRow> verbruikList = new List<VerbruikRow>();     
    }

    public class VerbruikRow
    {
        public string Datum { get; set; }
        public string Liter { get; set; }
        public string Km { get; set; }
        public string Verschil { get; set; }
    }

Here is the code for the controller that I have:

// GET: Verbruik
        public ActionResult Index()
        {
            Verbruik model = new Verbruik();
            VerbruikRow rowtest = new VerbruikRow();
            rowtest.Datum = "1/1/2015";
            rowtest.Km = "2000";
            rowtest.Liter = "0";
            rowtest.Verschil = "0";
            model.verbruikList.Add(rowtest);
            model.Gemiddelde1 = "";
            model.Gemiddelde2 = "";
            return View();
        }

And here is the code of the view:

@model EfenkaMobile.Models.Verbruik
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>


<div class="row">
    <div class="col-md-10">
        <h3>
            @Html.DisplayFor(m => m.Gemiddelde1)
        </h3>
        <h3>@Html.DisplayFor(m => m.Gemiddelde1)</h3>

    </div>
</div>
<div class="row">
    <div class="col-md-10">
        <h3>
            @Html.DisplayFor(m => m.Gemiddelde2)
        </h3>
        <h3>@Html.DisplayFor(m => m.Gemiddelde2)</h3>

    </div>
</div>

</br >

@foreach (var x in Model.verbruikList)
{<li>@x.Datum</li>
    <li>@x.Km</li>
    <li>@x.Liter</li>
    <li>@x.Verschil</li>}

This is all test code for now, but eventually I will get data from the database and use that to fill the verbruiklist with rows of data. Then I want to show this data in the view, with a pagination nugget.

When I try to run this, I get an error when I get to the razor foreach loop.

This is the error that I get when I try to run it:

An exception of type 'System.NullReferenceException' occurred in App_Web_2rvwlpox.dll but was not handled in user code

Additional information: Object reference not set to an instance of an object.
Robin
  • 2,704
  • 7
  • 30
  • 47
  • 2
    You don't return the model to the view so `verbruikList` is `null`. It needs to be `return View(model);` –  Aug 17 '15 at 09:04
  • 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) –  Aug 17 '15 at 09:05

1 Answers1

2

You have to return your model to the view:

public ActionResult Index()
{
            Verbruik model = new Verbruik();
            VerbruikRow rowtest = new VerbruikRow();
            rowtest.Datum = "1/1/2015";
            rowtest.Km = "2000";
            rowtest.Liter = "0";
            rowtest.Verschil = "0";
            model.verbruikList.Add(rowtest);
            model.Gemiddelde1 = "";
            model.Gemiddelde2 = "";
            return View(model);
 }

Becuase your view needs it:

@model EfenkaMobile.Models.Verbruik
Sirwan Afifi
  • 10,654
  • 14
  • 63
  • 110