我有一个学生数据模型(实体框架),我已经将“StudentID”和“StudentName”设置为主键。 StudentID的类型为Int,StudentName的类型为String。空引用异常错误
我创建了一个强类型的视图,但是当我运行它,我得到以下错误:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error: Line 10: <tr> Line 11: <td> Line 12: <%= Html.Encode(item.StudentID) %>** Line 13: </td> Line 14: <td>
这里是我的控制器操作:
public ActionResult Index()
{
ViewData.Model = student.StudentTable;
return View();
}
这里查看:
<%@ Page
Language="C#"
Inherits="System.Web.Mvc.ViewPage<IEnumerable<Student.Models.StudentTable>>" %>
<html>
<head runat="server">
</head>
<body>
<table>
<% foreach (var item in Model) { %>
<tr>
<td>
<%= Html.Encode(item.StudentID) %>
</td>
<td>
<%= Html.Encode(item.StudentName) %>
</td>
</tr>
<% } %>
</table>
</body>
</html>
你能显示你的Controller Action方法的代码吗? – brodie
@brodie是的,我在 – user793468
上面加了它,你能告诉我们你的看法吗? – frennky