MVC でデータベースに情報を送信し、ページをリダイレクトして、新しく保存されたデータを出力するビューを呼び出すと、NullReferenceException エラーが発生します。
"オブジェクト参照がオブジェクト インスタンスに設定されていません。"
どうやら、私のモデルはnullです:
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.CustomerID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
これは私のコントローラーです:
public ActionResult Index()
{
var customer = db.Customer.ToList();
return View();
}
呼び出されるのは次のとおりです。
[HttpPost]
public ActionResult Create(CustomerModel customer)
{
db.Customer.Add(customer);
db.SaveChanges();
return RedirectToAction("Index");
}
なんでヌルなのかわからない…。