我正在使用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; }
}
现在我想将其部分字段传递给Index视图。我该怎么做?
以下是有关其他表格的更多信息:
1-Lead Class:
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视图吗?如果是这样,怎么样? 我应该如何在索引视图中使用它?
答案 0 :(得分:0)
为视图模型创建一个实例并将其传递给视图
LeadViewModel model = new LeadViewModel();
//Populate model
return View(model);
答案 1 :(得分:0)
你不需要任何额外的信息,除了Lead及其相关的手机和电子邮件,然后你可以直接从实体框架传递它:
public ActionResult SomeAction()
{
IEnumerable<Leads> model = db.Leads.Include(i => i.LeadPhones).Include(n => n.LeadEmails);
return View(model);
}
在视图中:
@model IEnumerable<Leads>
foreach(Leads lead in Model)
{
foreach(Phones phone in lead.LeadPhones)
{
<div>@phone.PhoneNo</div>
}
foreach(LeadEmails email in lead.LeadEmail)
{
// similarly with emails
}
}