如何使此剃须刀视图正常工作



在我的MVC Web应用程序中,这是使用iCollection对象的模型,

public class EmpProfile
{
    public int EmpProfileID { get; set; }
    public string EmpName { get; set; }
    public string EmpNum { get; set; }
    public string ManagerEditor { get; set; }
    public string DocCreatedBy { get; set; }
    public virtual ICollection<PerfPlan> PerfPlans { get; set; }
    public virtual ICollection<ProgReview> ProgReviews { get; set; }
 }

这是PerfPlan模型,另一个模型ProgReviews与此类似。

public class PerfPlan
     {
        public int ID { get; set; }
        public int EmpProfileID { get; set; }
        public string EmpName { get; set; }
        public string EmpNum { get; set; }
        public string Title { get; set; }
        ....
         public virtual EmpProfile EmpProfile { get; set; }
    }

基本上,它在EmpProfile和PerfPlan,ProgReview之间建立了一对多的关系。因此,一个 EmpProfile 具有 0 个或多个性能计划和进度审核数据(模型)。现在,在我的 EmpProfile 索引剃刀中,我想列出与每个 EmpProfile 相关的所有 PerfPlan 和 ProgReview,我构建了这样的东西:

@model IEnumerable<PerfM.Models.EmpProfile>
<table>
@foreach (var item in Model) 
{
<tr class="@selectedRow">
<td >
        @Html.ActionLink(@item.EmpName, "Index", new { id = item.EmpProfileID  })
    </td>
</tr>
 //here I need to list all PerfPlan and ProgReview related to this EmpProfile and list under this row.

任何专家可以帮助我继续下面的代码吗?多谢

只需使用像这样的简单 foreach 循环(在您的 foreach 循环内部):

foreach(var plan in item.PerfPlans)
{
     // here you can access your PerfPlan properties like:
   <tr>
   <td> @plan.Id </td>
   <td> @plan.EmpName</td>
   <td> @plan.EmpNum </td>
   ...
   </tr>
}
foreach(var review in item.ProgReviews)
{
     ...
}

在您的控制器中,不要忘记包含您的集合:

var profiles  = context.EmpProfiles.Include("PerfPlans").Include("ProgReviews");

最新更新