0

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)
Dea
  • 281
  • 3
  • 6
  • 16

2 Answers2

0

Changing you Create action to:

public ActionResult Create()
{
    var viewModel = new UniversityApp.Models.Student();
    return View(viewModel);
}

will fix the immediate problem.

swatsonpicken
  • 631
  • 1
  • 4
  • 18
  • Create method takes as view a Student model. Please look at my question above, the model which View takes is a Student – Dea Jan 11 '17 at 10:40
  • Yep, my bad. I have edited the answer to correct this. – swatsonpicken Jan 11 '17 at 11:05
  • also I must provide a paramter for method Creat. The answer posted by Georg Patscheider is completly right. I need this argument professorId because in my View of Index method withoud that parameter another exception will be thrown – Dea Jan 11 '17 at 11:51
0

As swatsonpicken remarked, you have to pass a ViewModel to the View in the GET Create action. This action probably has to accept a parameter for the Professor.

 [HttpGet]
 public ActionResult Create(int professorId) {
     var vm = new  UniversityApp.Models.Student {
         professorId = professorId 
     }
     // best practice: always provide the name of the View you want to render
     return View("Create", vm); 
 }
Georg Patscheider
  • 8,808
  • 1
  • 21
  • 34