Get data from the database using mvc

asp.net-mvc c# entity-framework model-view-controller

Question

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) {

1
1
3/7/2014 9:27:02 AM

Accepted Answer

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>
    }
1
3/7/2014 9:43:18 AM

Popular Answer

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);
    }
}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow