【发布时间】:2015-11-07 19:52:56
【问题描述】:
我在 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; }
}
基本上我有两个外键:类型和地址。我已经能够将一些信息插入到数据库中,如下所示:
所以数据库包含信息,但每次我调用索引视图来列出所有属性时,我都会得到一个 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>
编辑:空值来自 Controller 类,就像这样:
public ActionResult Index()
{
return View(db.Properties.ToList());
}
我能做什么?
【问题讨论】:
标签: c# asp.net-mvc entity-framework razor