3

So I am creating a strongly-typed view. My model is called RestaurantReview.cs and looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace OdeToFood.Models
{
    public class RestaurantReview
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string City { get; set; }
        public string Country { get; set; }
        public int Rating { get; set; }
    }
}

I had Visual Studio create a strongly-typed List model based on this, which looks like this:

@model IEnumerable<OdeToFood.Models.RestaurantReview>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.City)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Country)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Rating)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.City)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Country)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Rating)
        </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>
}

</table>

When I run the site, I get a null pointer exception in the line "@foreach (var item in Model)", highlighting Model object, and stating "Object reference not set to an instance of an object."

Not really understanding how this code can be wrong since I didn't even write it, Visual Studio did. What is happening here?

Red
  • 2,728
  • 1
  • 20
  • 22
jimboweb
  • 4,362
  • 3
  • 22
  • 45

2 Answers2

2

Sounds like you haven't instantiated your model correctly within the Controller.

As a test you could try this:

public ActionResult Reviews()
{
   var model = new List<OdeToFood.Models.RestaurantReview>();
   model.Add(new OdeToFood.Models.RestaurantReview { Name = "Test" });
   model.Add(new OdeToFood.Models.RestaurantReview { Name = "Test2" });

   return View(model);
}

However the model should be correctly populated from the DB. If you can paste your Controller code that would help.

Darren
  • 68,902
  • 24
  • 138
  • 144
2

Your Controller show have the RestaurantReview IEnumerable passed. Example:

public class HomeController : Controller { //suppose this is your Home
    public ActionResult Index() {
        IEnumerable<OdeToFood.Models.RestaurantReview> model;
        model = from m in db.RestaurantReviews
                ... //your query here
                select m;
        return View(model); //pass the model here
    }

Then you will not get null exception

Ian
  • 30,182
  • 19
  • 69
  • 107
  • I see what I did. I forgot to include the parameter in "return View()." When I changed it to "return View(model)" it worked. Thanks! – jimboweb Mar 19 '16 at 15:05
  • @jimboweb great! ;) yes, that is typically the issue... – Ian Mar 19 '16 at 15:06