1

I am trying to retrieve data from the database and populate the records in a table, but i am getting an error An exception of type 'System.NullReferenceException' occurred in App_Web_14paz0pq.dll but was not handled in user code Additional information: Object reference not set to an instance of an object. I get this error when running my project, my view dont even show-up in web-browser. This is my model:

public class MovieModel
{
    public int ID { set; get; }
    [DisplayName("First Name")]
    public string FirstName { set; get; }
    [DisplayName("Second Name")]
    public string SecondName { set; get; }
    [DisplayName("Date Of Birth")]
    public DateTime DOB { set; get; }
    [DisplayName("Other Info")]
    public string Other{ set; get; }
}

This is my controller:

namespace Movie.Controllers
{
    public class MoviesController : Controller
    {

        private ApplicationDbContext db = new ApplicationDbContext();

        public ActionResult GetList() {
        return View();
        }

        [HttpPost]
        public ActionResult GetList(MovieModel model)
        {
            var data = db.Movies.ToList();
            return View(data);
        }
    }
}

This is my View:

@model IEnumerable<Movie.Models.MovieModel>

@{
   ViewBag.Title = "";
 Layout = "~/Views/Shared/_Layout.cshtml";
    }  


<p>
    @Html.ActionLink("Create New", "Create")
 </p>
   <table class="table">
    <tr>
    <th>
        @Html.DisplayNameFor(model => model.FirstName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.SecondName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.DOB)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Other)
    </th>
    <th></th>
</tr>



@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.FirstName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.SecondName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.DOB)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Other)
    </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>

I am getting this error in this line @foreach (var item in Model) {

8
  • Quick watch db.Movies Commented Mar 7, 2014 at 9:07
  • 1
    What is db? Is it initialized? Commented Mar 7, 2014 at 9:08
  • @Rex & Kjartan i've updated my question Commented Mar 7, 2014 at 9:10
  • try to move GetList Action into your Controller Commented Mar 7, 2014 at 9:14
  • @fofik: It is already in the Controller, its just bad alignment. :) Commented Mar 7, 2014 at 9:15

3 Answers 3

2

Try like this,

public class MoviesController : Controller
{

  private ApplicationDbContext db = new ApplicationDbContext();

  public ActionResult GetList() 
  {
    var Movies = (from movie in db.Movies select movie).ToList();
    return View(Movies);
  }

  [HttpPost]
  public ActionResult GetList(MovieModel model)
  {
   var data = db.Movies.ToList();
    return View(data);
  }

View

@model IEnumerable<Movie.Models.MovieModel>

@{
   ViewBag.Title = "";
 Layout = "~/Views/Shared/_Layout.cshtml";
    }  


<p>
    @Html.ActionLink("Create New", "Create")
 </p>
   <table class="table">
    <tr>
    <th>
        @Html.DisplayNameFor(model => model.FirstName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.SecondName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.DOB)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Other)
    </th>
    <th></th>
</tr>

@if (Model != null)
{

foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.FirstName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.SecondName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.DOB)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Other)
    </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>
    }
Sign up to request clarification or add additional context in comments.

6 Comments

This works, i dont had to place @IF in View. But what if i would like to select only FirstName, SecondName and DOB from the database. How would my module look then?
@User911 @If is just make sure that if your model is null so it don't give you error page.and if you have to display only FirstName, SecondName and DOB field in your page right? or you have to only retrieve this three field from database?
i am getting repeated results why is that? can i not select distinct records by their name?
@User911 it display repeated result because same name store many time in database. Yes you can select distinct record in your query.
Just for the experience purpose, if i had a ID for each record is this how i select a distinct record ` var Movies = (from movie in db.Movies select movie).Distinct().ToList();` where would i place ID so that it selects distinct record based on id?
|
1

You are getting System.NullReferenceException here

@foreach (var item in Model) becase you are not passing a strongly-typed model to your view. So the Model in the for loop is NULL.

Try this in your controller:

public class MoviesController : Controller
{

    private ApplicationDbContext db = new ApplicationDbContext();

    public ActionResult GetList() 
    {
        var model = db.Movies.ToList();
        return View(model);
    }

    [HttpPost]
    public ActionResult GetList(MovieModel model)
    {
        var data = db.Movies.ToList();
        return View(data);
    }
}

2 Comments

i get error: An exception of type 'System.NotSupportedException' occurred in mscorlib.dll but was not handled in user code Additional information: The entity or complex type 'Movie.Models.MovieModel' cannot be constructed in a LINQ to Entities query.
first posted answer and the same answer as the accepted answer. :)
0

Assuming it is a Razor View and Movies/GetList is your default Url,you might be trying to use the Model object directly in the view without a Null check. Please check your cshtml C# code.

If the null reference were in a .cs file, you wouldn't see App_Web_*.dll since that is the DLL for the compiled pages.

Comments

Your Answer

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