I have a Student data model(Entity Framework) where I have set both “StudentID” and “StudentName” as primary keys. StudentID is of type Int and StudentName is of type String.
I created a strongly-typed view, But when I run it i get the following error:
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>
Here is my controller action:
public ActionResult Index()
{
ViewData.Model = student.StudentTable;
return View();
}
Here is the 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>
Without any additional information, my guess is that
itemisnull. If the Student Table has a SINGLE StudentID per record, then you need to simply passmodel.StudentIDController
aspx