【发布时间】:2014-12-12 07:29:36
【问题描述】:
我目前正在尝试开发我的第一个 .NET MVC 应用程序并学习主要概念。
在我的应用程序中,我有一个表格,显示动物表格中的动物列表。在表中,我也试图显示动物品种,但我正在从动物表中存储的外键的品种表中提取品种
我目前正在尝试使用导航属性来显示品种文本而不是 ID,所以我 将我的 Animal 模型更改为如下所示
public partial class Animal
{
public int AnimalId { get; set; }
public Nullable<int> UserId { get; set; }
public string TagNo { get; set; }
public Nullable<int> Species { get; set; }
public Nullable<bool> Sex { get; set; }
public Nullable<int> AnimalBreed { get; set; }
public Nullable<System.DateTime> DOB { get; set; }
public Nullable<int> OwnershipStatus { get; set; }
public Nullable<System.DateTime> DateAdded { get; set; }
public Nullable<bool> BornOnFarm { get; set; }
public virtual Breed Breed { get; set; }
}
我的品种模型看起来像
public partial class Breed
{
public int id { get; set; }
public Nullable<int> SpeciesID { get; set; }
public string Breed1 { get; set; }
}
在我看来,我正试图从我的动物模型中显示 Breeds 字段,如下所示,但品种列只是空的
<td>
@Html.DisplayFor(modelItem => item.Breed.Breed1)
</td>
最后,这是我用来将模型发送到视图的代码
List<Animal> Animal1 = (from animals in db.Animals
where animals.Species == 2 && animals.OwnershipStatus == 1
&& animals.UserId == WebSecurity.CurrentUserId
select animals).ToList();
return View(Animal1);
【问题讨论】:
标签: asp.net-mvc entity-framework navigation-properties