我有一個基本的MVC視圖,將列出我所有的屬性。屬性模型編碼是這樣的:剃刀當試圖顯示項目時得到空引用項目
public class Property
{
public int Id { get; set; }
[Display(Name = "Type")]
public PropertyType Type { get; set; }
[Display(Name = "Address")]
public Address Address { get; set; }
[Display(Name = "Area")]
[Required(ErrorMessage = "A property area is required.")]
public double Area { get; set; }
}
基本上我有2個外鍵:類型和地址。我已經能夠插入一些信息到數據庫中,如下圖所示:
因此數據庫中包含的信息,但每次我打電話索引視圖列出所有的屬性,我得到一個NullReferenceException。
@foreach (var item in Model) {
<tr>
<td>
@Html.Display(item.Type.Id.ToString())
</td>
<td>
@Html.Display(item.Address.Id.ToString())
</td>
<td>
@Html.DisplayFor(modelItem => item.Area)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
編輯:空值從控制器正在添加類,就像這樣:
public ActionResult Index()
{
return View(db.Properties.ToList());
}
我能做些什麼?
你先生是男人。非常感謝你! –