【发布时间】:2014-04-10 08:13:20
【问题描述】:
我正在尝试从数据库中检索数据并将记录填充到表中,但我收到一个错误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. 我在运行我的项目时收到此错误,我的视图甚至没有显示在网络浏览器中。这是我的模型:
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; }
}
这是我的控制器:
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);
}
}
}
这是我的观点:
@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>
我在@foreach (var item in Model) {这一行收到此错误
【问题讨论】:
-
快看db.Movies
-
什么是
db?初始化了吗? -
@Rex & Kjartan 我更新了我的问题
-
尝试将 GetList Action 移动到您的控制器中
-
@fofik:它已经在控制器中,只是对齐不好。 :)
标签: c# asp.net-mvc entity-framework model-view-controller