While I am running my ASP.NET MVC application I am running into an exception "Object reference not set to an instance of an object." and the line where it breaks is :
<div>
@Html.ActionLink("Back","Index", new { id = @Model.professorId })
</div>
This line of code is in /Views/StudentEnroll/Create.cshtml and the file Create.cshtml has the code below:
@model UniversityApp.Models.Student
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Student</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.City, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.City, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.City, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
//here it breaks
<div>
@Html.ActionLink("Back","Index", new { id = @Model.professorId })
</div>
The action method Index in StudentEnroll controller is as below:
public class StudentEnrollController : Controller
{
private UniversityInitial studentDataSet = new UniversityInitial();
public ActionResult Index([Bind(Prefix="id")] int professorId)
{
var prof = studentDataSet.Professors.Find(professorId);
return View(prof); // returns a single instance of the professor.So view of the index must not be Ienumerable
}
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,FirstName,LastName,City,professorId")]Student student)
{
if (ModelState.IsValid)
{
studentDataSet.Students.Add(student);
studentDataSet.SaveChanges();
return RedirectToAction("Index",new { id = student.professorId });
}
{
return View(student);
}
}
The View of the Index method is as below:
@model UniversityApp.Models.Professor
<h2>Student enrolled in the subject: @Model.Subject of @Model.FirstName @Model.Lastname</h2>
<p>
@Html.ActionLink("Enroll New Student", "Create", new { professorId = @Model.Id })
</p>
<!--students is a property of the Professor model of type List-->
@Html.Partial("_StudentEnroll",@Model.students)