简体   繁体   中英

Get data from the database using mvc

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

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM