在视图中显示列表MVC 4

时间:2016-08-01 11:55:27

标签: asp.net-mvc asp.net-mvc-4

我有一个模型课程,一个模型组件和一个模型评估:

public class Course
 {
   public virtual int CourseId { get; set; }
   public virtual string Name { get; set; }
   public virtual List<Component> Components { get; set; }
   public virtual List<UserProfile> Users { get; set; }
   public virtual List<Evaluation> Evaluation { get; set; }
 }

public class Component
 {
   public virtual int ComponentId { get; set; }
   public virtual int CourseId { get; set; }
   public virtual Course Course { get; set; }
   public virtual string NameComp { get; set; }
 }

public class Evaluation
{

   public virtual int EvaluationId { get; set; }

   public virtual int CourseId { get; set; }
   public virtual Course Course { get; set; }

   public virtual int UserId { get; set; }
   public virtual UserProfile User { get; set; }

   public virtual int Grade { get; set; }
}

我需要在视图中显示一个包含所有用户的表格,创建的所有组件以及每个用户的成绩。 我试过这种方式:

@model SGP.Models.Course

<table>
<tr>
    <th>
        Username
    </th>
    @foreach (var x in Model.Components)
    {
        <th>
            @Html.DisplayFor(modelItem => x.NameComp)
        </th>
    }
    <th></th>
</tr>

@foreach (var item in Model.Evaluation)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.User.UserName)
        </td>
        @foreach (var x in Model.Components)
        {

            <td>
                @Html.DisplayFor(modelItem => item.Grade)
            </td>
        }
        <td>

        </td>
    </tr>
}

但是这给了我一列中的所有组件,每个组件需要一个列,每个组件都有一个等级:

我的代码给出了这个:

Username - Component1Component2Component3
   Test  -              12 
   Test  -              13
   Test  -              10
(example of result with username and grades)

我需要:

Username - Component1 - Component2 - Component3
  Test   -    12      -    13      -    10 
(example of result with username and grades)

我该怎么做?感谢

2 个答案:

答案 0 :(得分:0)

课程评估的定义应为:

public virtual IGrouping<string, Evaluation> Evaluations { get; set; }

代码应为:

@model SGP.Models.Course

<table>
<tr>
    <th>
        Username
    </th>
    <th>
    @foreach (var x in Model.Components)
    {
        @Html.DisplayFor(modelItem => x.NameComp)
    }
    </th>
    <th></th>
</tr>
@foreach (var group in Model.Evaluations)
{
<tr>
    <td>
       @Html.DisplayFor(modelItem => group.Key)
    </td>
    @foreach (var item in group)
    {
        <td>
          @Html.DisplayFor(modelItem => item.Grade)
        </td> 
    }
    <td>
    </td>
}
</tr>

答案 1 :(得分:0)

您需要确保评估参考课程组件:

public class Evaluation
{

    public virtual int EvaluationId { get; set; }

    public virtual int CourseId { get; set; }
    public virtual Course Course { get; set; }

    public virtual int ComponentId { get; set; }
    public virtual Component Component { get; set; }

    public virtual int UserId { get; set; }
    public virtual UserProfile User { get; set; }

    public virtual int Grade { get; set; }
}

以下是一些测试值:

var users = new List<UserProfile>
{
    new UserProfile { UserId = 1, UserName = "Jim" },
    new UserProfile { UserId = 2, UserName = "Sam" },
};

var components = new List<Component>
{
    new Component { ComponentId = 1, NameComp = "Unit 1" },
    new Component { ComponentId = 2,  NameComp = "Unit 2" }
};

var course = new Course
{
    Users = users,
    Components = components.OrderBy(c => c.ComponentId).ToList(),
    Evaluation = new List<Evaluation>
    {
        new Evaluation { User = users[0], ComponentId = components[0].ComponentId, Grade = 87 },
        new Evaluation { User = users[0], ComponentId = components[1].ComponentId, Grade = 99 },                   
        new Evaluation { User = users[1], ComponentId = components[0].ComponentId, Grade = 75 }, 
        new Evaluation { User = users[1], ComponentId = components[1].ComponentId, Grade = 65 }
    }
};

遵循Razor视图应该做你需要的:

    @model SGP.Models.Course

<table>
    <tr>
        <th>
            Username
        </th>
        @foreach (var x in Model.Components)
        {
            <th>
                @Html.DisplayFor(modelItem => x.NameComp)
            </th>
        }
        <th></th>
    </tr>

    @foreach (var userEvaluation in Model.Evaluation.GroupBy(e => e.User))
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => userEvaluation.Key.UserName)
            </td>
            @foreach (var x in Model.Components)
            { 
                <td>
                    @(userEvaluation.FirstOrDefault(e => e.ComponentId == x.ComponentId)?.Grade)
                </td>
                }
        <td></td>
    </tr>
}

注意这个模型,它有很多循环引用,如果你使用像Entity Framework这样的东西,这可能会有问题。