我正在寻找一个主细节视图。
我有两张表 tbFamily 和 tbFamilyMember。
我目前能够查看家庭详细信息并列出相关的家庭成员,但是,我想根据他们的角色来分隔家庭成员。即我想显示父母/监护人列表,然后显示按出生日期排序的孩子列表。
我的模型如下:
public class tbFamily
{
public int tbFamilyID { get; set; }
public string Surname { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Address3 { get; set; }
public string Town { get; set; }
public string County { get; set; }
public string Postcode { get; set; }
public string Country { get; set; }
public string Telephone { get; set; }
public string Mobile { get; set; }
public string Email { get; set; }
public virtual ICollection<tbFamilyMember> tbFamilyMember { get; set; }
public virtual ICollection<tbFamilyData> tbFamilyData { get; set; }
}
public class tbFamilyMember
{
public int tbFamilyMemberID { get; set; }
public int tbFamilyID { get; set; }
public int Role { get; set; }
public string Firstname { get; set; }
public string Surname { get; set; }
public char Gender { get; set; }
[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
public DateTime DateOfBirth { get; set; }
public virtual tbFamily tbFamily { get; set; }
}
我的控制器目前看起来如下:
public ActionResult Details(int id = 0)
{
tbFamily tbfamily = db.tbFamily.Find(id);
if (tbfamily == null)
{
return HttpNotFound();
}
return View(tbfamily);
}
其关联的视图
@Html.DisplayFor(model => model.Surname)
@Html.DisplayFor(model => model.Address1)
@Html.DisplayFor(model => model.Address2)
@Html.DisplayFor(model => model.Address3)
@Html.DisplayFor(model => model.Town)
@Html.DisplayFor(model => model.County)
@Html.DisplayFor(model => model.Postcode)
@Html.DisplayFor(model => model.Country)
@Html.DisplayFor(model => model.Telephone)
@Html.DisplayFor(model => model.Mobile)
@Html.DisplayFor(model => model.Email)
@foreach (var item in Model.tbFamilyMember)
{
@Html.DisplayFor(modelItem => item.Firstname)
@Html.DisplayFor(modelItem => item.Surname)
@Html.DisplayFor(modelItem => item.Role)
}
获取孩子:
public ActionResult GetChildren(int id)
{
var tbFamilyData = from f in db.tbFamilyMember
where f.tbFamilyID.Equals(id)
where f.Role.Equals(3)
select f;
return View(tbFamilyData);
}
其关联视图:
@foreach (var item in Model.tbFamilyData)
{
@Html.DisplayFor(modelItem => item.tbFamilyMember.Firstname)
@Html.DisplayFor(modelItem => item.tbFamilyMember.Surname)
@Html.DisplayFor(modelItem => item.tbFamilyMember.Role)
}
只是不知道如何让两者一起工作!
我创建了一个单独的模型 tbFamilyData
public class tbFamilyData
{
public virtual tbFamily tbFamily { get; set; }
public virtual tbFamilyMember tbFamilyMember { get; set; }
}
我现在收到错误 tbFamilyData has no key defined。
希望有人可以提供帮助。