【问题标题】:How to pass ViewModel to Index view?如何将 ViewModel 传递给索引视图?
【发布时间】: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


【解决方案1】:

为视图模型创建一个实例并将其传递给视图

    LeadViewModel model = new LeadViewModel();
    //Populate model
    return View(model);

【讨论】:

  • 我的问题是“填充模型”部分。我应该如何从数据库中填充 viewModel?
【解决方案2】:

除了潜在客户及其相关电话和电子邮件之外,您不需要任何额外信息,然后您可以直接从实体框架传递它:

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
    }
}

【讨论】:

  • 谢谢。我会试试这个,然后告诉你结果。
  • 我是 MVC 的新手,但这行不通。因为“LeadPhones”表中没有“PhoneNo”。 “LeadPhones”只是一个联结表,仅包含 LeadID 和 PhoneID 而不是 PhoneNo。 PhoneNo 在“电话”表中。我不知道如何获取指定潜在客户的PhoneNo。
猜你喜欢
  • 1970-01-01
  • 2013-07-14
  • 2021-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-21
  • 2011-01-22
  • 1970-01-01
相关资源
最近更新 更多