2017-01-11 17 views
0

ASP.NET MVCアプリケーションを実行しているときに例外が発生しました。 。オブジェクト」と、それは改行は次のとおりです。例外を処理する方法オブジェクトの参照がオブジェクトのインスタンスに設定されていません。

<div> 
    @Html.ActionLink("Back","Index", new { id = @Model.professorId }) 
</div> 

このコード行は/Views/StudentEnroll/Create.cshtmlであり、ファイルCreate.cshtmlは、以下のコードがあります。

@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> 

をStudentEnrollコントローラのアクションメソッドのインデックスは次のとおりです。

Indexメソッドの
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); 
     } 
    } 

ビューは以下の通りです:

@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) 
+2

ビューモデルオブジェクトを作成ビューに渡していないため、モデルはnullです。 – swatsonpicken

答えて

0

swatsonpickenが注目しているので、GET CreateアクションのビューにViewModelを渡す必要があります。このアクションはおそらく教授のためのパラメータを受け入れなければならないでしょう。

[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); 
} 
0

はあなたにアクションを作成します。変更:

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

当面の問題を解決します。

+0

Createメソッドは、Studentモデルを表示します。上記の私の質問を見てください、ビューがかかるモデルは、学生です – Dea

+0

うん、私の悪い。私はこれを修正する答えを編集しました。 – swatsonpicken

+0

また、Creatメソッドのパラメータを指定する必要があります。 Georg Patscheiderによって投稿された回答は完全に正しいです。私はこの引数professorIdが必要です。なぜなら、そのパラメータを持つIndexメソッドのViewでは、別の例外がスローされるからです – Dea

関連する問題