我使用Visual Studio在ASP中创建了一个非常基本的Web应用程序,然后使用默认网站创建了'Employee'模型。该模型可以使用以下方式存储在数据库中:从ASP页面列出模型,使用Visual Studio和C#
public class EmployeeDBContext : DbContext
{
public DbSet<Employee> Employees{ get; set; }
}
在Employee名称空间中。当我为此模型创建控制器时,将创建默认的创建,读取,更新和删除方法。还有一个索引页面是在页面第一次加载时显示的,这个页面显示了当前在数据库中的每个员工。对于Index.cshtml的代码看起来是这样的:
@model IEnumerable<AnotherWebApp.Models.Employee>
@{
ViewBag.Title = "Index";
}
<h2>All Employee Options</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<p>
@Html.ActionLink("View All", "ViewAll")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Role)
</th>
<th>
<b>Options</b>
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Role)
</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>
我所试图做的是显示在Index.cshtml一个基本的菜单,并链接到包含所有员工的表ViewAll页面。问题在于“对象引用未设置为对象的实例”。并且该页面不显示。我看不到为什么这个代码在Index.cshtml上工作,但不会在ViewAll.cshtml上工作,任何人都有建议?这里有一些链接指向一些教程:http://www.asp.net/mvc/tutorials/mvc-5/introduction/accessing-your-models-data-from-a-controller
感谢您的任何建议。
你能告诉我们实际产生错误的代码,并指出错误发生在哪一行上吗? 'NullReferenceException'非常容易调试,只需在该行上放置一个断点,并在调试时查看哪个对象为'null'。 – David 2014-10-08 15:46:39