我有一个包含三个表(DAL类)的数据结构,如下所示:
public class Subject
{
[Key]
public int SubjectId { get; set; }
public string Name { get; set; }
public string Company { get; set; }
public string Address { get; set; }
public virtual List<SubjectContact> SubjectContacts{ get; set; }
}
public class SubjectContact
{
[Key]
public int SubjectContactId { get; set; }
public int InfoTypeId { get; set; }
public virtual InfoType IntoType{ get; set; }
public string InTypeValue { get; set; }
public int SubjectId { get; set; }
public virtual Subject Subject{ get; set; }
}
public class InfoType
{
[Key]
public int InfoTypeId { get; set; }
public string TypeName { get; set; }
public int SubjectContactId { get; set; }
public virtual List<SubjectContact> SubjectContacts{ get; set; }
}
主题: SubjectId名称公司地址 101 John C1 A1 102 Michael C2 A2 103 Elizabeth C3 A3
SubjectContact: SubjectContactId InfoTypeId InfoTypeValue SubjectId 1001 1000001 16174350000 101 1002 1000002 16177891234 101 1003 1000003 john@gmail.com 101 1004 1000001 13027546200 102 1005 1000004 lizAndHeather 102
信息类型 InfoTypeId TypeName 1000001手机 1000002家庭电话 1000003电子邮件 1000004 Skype
最终的视图将是这样的:
@model IEnumerable<FootViewModel>
<ul>
@foreach (var i in Model)
{
<li>
@i.Subjects
</li>
@foreach (var j in Model.Subjects)
{
<li>
@j.SubjectContacts
@Model.InfoType.TypeName
</li>
}
}
</ul>
FootViewModel应如下所示:
public class FootViewModel
{
public Subject Subject { get; set; }
public List<Subject> SubjectContactInfos { get; set; }
public List<InfoType> InfoTypes { get; set; }
}
但这是错误的,因为它缺少SubjectContacts和InfoTypes之间的关系。你能告诉我如何构建viewmodel FootViewModel吗?谢谢!
答案 0 :(得分:0)
感谢斯蒂芬的纠正。主题包含subjectContacts列表。我想我给出了错误的描述。实际上结构应该是这样的:
主题:
SubjectId名称公司地址
101 John C1 A1
102 Michael C2 A2
103 Elizabeth C3 A3
SubjectContact:
SubjectContactId InfoTypeId InfoTypeValue SubjectId
1001 1000001 16174350000 101
1002 1000002 16177891234 101
1003 1000003 john@gmail.com 101
1004 1000001 13027546200 102
1005 1000004 lizAndHeather 102
信息类型
InfoTypeId TypeName
1000001手机
1000002家庭电话
1000003电子邮件
1000004 Skype
我创建了一个中间ViewModel ContactValueViewModel,如下所示,希望这会有效:
public class FootViewModel
{
public SubjectViewModel Subject { get; set; }
public List<ContactValueViewModel> ContactValues{ get; set; }
}
public class ContactValueViewModel
{
public SubjectContactInfoViewModel SubjectContactInfo { get; set; }
public InfoTypeViewModel InfoTypes { get; set; }
}