组合来自多个表的数据以在 MVC 视图中显示

Combining data from multiple tables to display in the MVC View

我的 MVC 项目中有两个独立的模型

[Table("Table1")]   
 public class Companies : IEnumerable
 {      
     [Key]
    public double ID { get; set; }
    public System.Guid Key { get; set; }
    public string Name { get; set; }
    public string Company { get; set; }
    public string Industry { get; set; }
    public string Rank{ get; set; }


    public IEnumerator GetEnumerator()
    {
        yield return this.ID;
        yield return this.Key;
        yield return this.Name;
        yield return this.Company;
        yield return this.Industry;
        yield return this.Rank;

    }
 }

[Table("Table2")]
 public class Registration : IEnumerable
 {
    [Key]
    public System.Guid Key { get; set; }
    public string FieldValue{ get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }


    public IEnumerator GetEnumerator()
    {
        yield return this.Key;
        yield return this.FieldValue;
        yield return this.StartDate;
        yield return this.EndDate;

     }
  }

我需要在我的视图中显示数据组合:

SELECT t2.FieldValue, t1.Name, t1.Company, t1.Industry, t1.Rank 
FROM Table1 as t1 INNER JOIN Table2 as t2 ON t1.Key=t2.Key

我创建了以下上下文 class:

public class TablesContext: DbContext
{
    public DbSet<Companies> companies { get; set; }
    public DbSet<Registration> registration { get; set; }
}

现在我可以使用以下代码将公司数据从控制器传递到视图:

private TablesContext tc = new TablesContext();
 var comps = (from c in tc.companies                                 
                    join r in tc.registry
                    on c.Key equals r.Key
      select new { FieldValue=r.FieldValue, Name=c.Name, Company=c.Company,Industry=c.Industry, Rank=c.Rank});
 return View(comps);

现在我有点卡住了,试图使 TablesContext class 可枚举并循环遍历视图中的结果集。谁能指出我正确的方向?

创建一个视图模型,即另一个 class 包含两个表中您需要的字段。然后用数据加载它,将其放入 LIST 并将其传递给视图。然后你可以使用 foreach 很容易地循环它。

这里解释的很好

祝你好运

您需要创建一个视图模型 - 请参见下面的示例:
加上使用完后尝试使用using语句处理上下文。

public class CompanyRegistrationModel
{
    public string FieldValue { get; set; }
    public string Name { get; set; }
    public string Company { get; set; }
    public string Industry { get; set; }
    public string Rank { get; set; }

    public CompanyRegistrationModel Get()
    {
        using (TablesContext tc = new TablesContext())
        {

        var comps = (from c in tc.companies
            join r in tc.registry
                on c.Key equals r.Key
            select
                new CompanyRegistrationModel
                {
                    FieldValue = r.FieldValue,
                    Name = c.Name,
                    Company = c.Company,
                    Industry = c.Industry,
                    Rank = c.Rank
                }).ToList();
        return comps;
        }
    }
}

您通常(可能)进入 ASP.NET MVC 的方式是创建一个像这样的 ViewModel:

public class CompanyRegViewModel{
  public string FieldValue {get;set;};
  public string Name {get;set;};
  public string Company {get;set;};
  public string Industry {get;set;};
  public string Rank {get;set;};
}

然后,您可以像这样创建 CompanyRegViewModel class 的实例,而不是创建匿名对象:

var comps= ..... select new CompanyRegViewModel{ FieldValue=r.FieldValue, Name=c.Name....};

另一方面,您想创建一个模型是 CompanyRegViewModel 列表的视图。 (但不必是 List<>。它可以是实现 IEnumerable<> 的任何东西)。这是您可能的视图的示例部分:

......
<table>
foreach(var item in Model){
  <tr>
    <td>item.Name</td>
    <td>item.Company</td>
    <td>item.Industry</td>
    @*and so on*@
  </tr>
}
</table>