2014-03-28 15 views
1

我想使数据库中的数据来自2个表中的数据。我查了一些东西,我试图使用ViewModel,但我无法让它工作。然后我考虑使用视图和局部视图来显示2个表中的数据。什么是使用ViewModel或使用视图和局部视图的最佳方式?MVC:在1视图中显示来自2个表的数据的最佳方式

此外,如果有人知道我在做什么我的ViewModel错误,并想帮助,这将是可怕的。

我的2种型号:

public partial class prospect 
{ 
    public prospect() 
    { 
     this.result = new HashSet<result>(); 
    } 

    public int id { get; set; } 
    public int testid { get; set; } 
    public string name { get; set; } 
    public string surname { get; set; } 
    public string email { get; set; } 
    public string cellnumber { get; set; } 
    public System.DateTime timestampstart { get; set; } 
    public Nullable<System.DateTime> timestampend { get; set; } 
    public Nullable<int> totalresult { get; set; } 
    public string birthdate { get; set; } 
    public string school { get; set; } 
    public Nullable<int> grade { get; set; } 
    public string address { get; set; } 
    public string cityandcode { get; set; } 

    public virtual ICollection<result> result { get; set; } 
    public virtual resultpersection resultpersection { get; set; } 
} 

public partial class resultpersection 
    { 
    public int id { get; set; } 
    public int prospectid { get; set; } 
    public int sectionid { get; set; } 
    public Nullable<int> result { get; set; } 

    public virtual prospect prospect { get; set; } 
    public virtual section section { get; set; } 
} 

我想要显示的东西: prospect.name,prospect.surname,prospect.totalresult和所有每这一前景的部分结果(这来自resultpersection表)

我的视图模型:

namespace testAptitude.Models 
{ 
    public class ResultsPerProspect 
    { 

    public List<prospect> prospect { get; set; } 
    public List<resultpersection> resultPerSection { get; set; } 
} 
} 

我控制器

public ActionResult Index() 
    { 
     ResultsPerProspect vm = new ResultsPerProspect(); 
     vm.prospect = (from p in db.prospect select p).ToList(); 
     vm.resultPerSection = (from rps in db.resultpersection select rps).ToList(); ; 
     List<ResultsPerProspect> viewModelList = new List<ResultsPerProspect>(); 

     viewModelList.Add(vm); 


     return View(viewModelList.AsEnumerable()); 
    } 

我看来

@model IEnumerable<testAptitude.Models.ResultsPerProspect> 

@{ 
    ViewBag.Title = "Index"; 
} 

<h2>Index</h2> 
<table> 
    <thead> 
     <tr> 
     <th class="col-sm-3"> 
      @Html.DisplayNameFor(model => model.prospect) 
     </th> 
    </tr> 
</thead> 
<tbody> 
    @foreach (var item in Model) 
    { 
     <tr> 
      <td> 
       @Html.DisplayFor(model => item.prospect) 
      </td> 
     </tr> 
    } 
    </tbody> 
</table> 

我不能这样做item.prospect.name,因为它说的是不包含定义名称

和现在dispalys是这样的:

前景

System.Collections.Generic.HashSet`1 [testAptitude.Models.result]

提前致谢!

回答

1

根据您目前的代码,你可以使用

@Html.DisplayNameFor(model => model.FirstOrDefault().prospect) 

你传入IEnumerable<testAptitude.Models.ResultsPerProspect>到您的视图ResultsPerProspect类的即多个对象访问它。你需要迭代这个List。此列表中的每个项目将包含prospectresultPerSection的定义。

或者您可以传递类ResultsPerProspect的单个对象,因为您只是在列表中添加单个元素。

UPDATE 您有ResultsPerProspect列表。并且ResultsPerProgpect的每个项目都有列表prospect和列表resultPerSection。所以你需要首先遍历ListPolyProspect。而内部的for循环,for循环的prospect列表和resultPerSection

代码清单

@foreach (var item in Model) 
    { 

    foreach (var pros in item.prospect) 
     { 
      <tr> 
       <td> 
        @Html.DisplayFor(model => pros.Name) 
       </td> 
      </tr> 
     } 
    } 
+0

你能解释一下吗?我已经通过foreach迭代了列表?你能给一个代码示例吗? – kicked11

+0

请检查我的更新 –

+0

oke谢谢,这就是我所需要的! – kicked11

0

你为什么不创建一个类,包括你的两个(子)类(“容器”) (比方说A和B)?然后,您可以创建一个Container对象,并将所需的对象放在Container.A和Container.B中。然后,您可以轻松地将“Container”传递给您的视图并访问您的对象。

+0

是不是viewModels是什么?一个新的班级,你放在两个分开的班级?我之前从未使用ViewModel,所以我不确定? – kicked11

相关问题