【发布时间】:2015-07-01 12:38:27
【问题描述】:
我正在使用 MVC 5 和 EF 6。 下面有一个 viewModel,我用它来创建视图:
public class LeadViewModel
{
public Leads _leads { get; set; }
public Addresses _addresses { get; set; }
public Phones _phones { get; set; }
public Emails _emails { get; set; }
public SelectList EmployeesList { get; set; }
public SelectList IndustryList { get; set; }
public SelectList LeadSourceList { get; set; }
public SelectList LeadStatusList { get; set; }
public SelectList AddressTypeList { get; set; }
public SelectList CityList { get; set; }
public SelectList PhoneTypeList { get; set; }
}
现在我想将它的一些字段传递给索引视图。我该怎么做?
这里是关于其他表的更多信息:
1-领导班:
public partial class Leads
{
public Leads()
{
this.LeadAddresses = new HashSet<LeadAddresses>();
this.LeadContacts = new HashSet<LeadContacts>();
this.LeadEmails = new HashSet<LeadEmails>();
this.LeadPhones = new HashSet<LeadPhones>();
}
[Key]
public int LeadID { get; set; }
public string NamePrefix { get; set; }
public string Title { get; set; }
public string FirstName { get; set; }]
public string LastName { get; set; }
[NotMapped]
public string FullName { get { return NamePrefix + " " + FirstName + " " + LastName; } }
public int EmployeeID { get; set; }
public int LeadStatusID { get; set; }
public virtual Employees Employees { get; set; }
public virtual ICollection<LeadEmails> LeadEmails { get; set; }
public virtual ICollection<LeadAddresses> LeadAddresses { get; set; }
public virtual ICollection<LeadPhones> LeadPhones { get; set; }
public virtual LeadStatus LeadStatus { get; set; }
}
电话类:
public partial class Phones
{
public Phones()
{
this.LeadPhones = new HashSet<LeadPhones>();
}
[Key]
public int PhoneID { get; set; }
public int PhoneTypeID { get; set; }
public string PhoneNo { get; set; }
public bool IsAllowed { get; set; }
public virtual ICollection<LeadPhones> LeadPhones { get; set; }
public virtual PhoneTypes PhoneTypes { get; set; }
}
LeadPhones 接线表:
public partial class LeadPhones
{
[Key]
public int LeadPhoneID { get; set; }
public int LeadID { get; set; }
public int PhoneID { get; set; }
public virtual Leads Leads { get; set; }
public virtual Phones Phones { get; set; }
}
在索引视图中我只想显示:
Leads.FullName
Leads.Title
Leads.LeadStatus.Status
Leads.Employees.FullName
Leads.Phones.PhoneNo
Leads.Industries.Industry
我应该将 viewModel 从控制器传递给 Index 视图吗?如果是这样,怎么做? 我应该如何在索引视图中使用它?
【问题讨论】:
-
那么...索引视图只需要一个潜在客户列表吗?为什么不直接传递一个 IEnumerable
呢?如果它需要更多,只需创建另一个视图模型。 -
Leads 和 LeadPhones 和 LeadEmails。我会创建另一个视图模型,但我不知道如何加载视图模型并将其传递给视图。
-
看起来 LeadPhones 和 LeadEmails 已经通过导航属性(虚拟关键字)附加到 Leads 对象。
-
@br4d:我觉得我解释得不好。有“Leads”、“Phones”表和“LeadPhones”接线表。现在我想显示“Leads”表中的潜在客户姓名和“Phones”表中的潜在客户电话,它们在“LeadPhones”表中连接。
-
是的,我明白这一点。您可以按照我在下面回答中的建议进行操作,也可以将它们展平为新的视图模型。无论哪种方式都有效。
标签: asp.net-mvc razor viewmodel