如何将来自多个模型的数据绑定到单个视图中的列表

时间:2014-11-11 21:59:09

标签: c# asp.net-mvc-5 entity-framework-6

我是Mvc的新手,但我找不到任何有用的例子。我有几个模型,我将其包装到Viewmodel中。我的目标是遍历每个项目并在表格中显示数据,然后添加CRUD功能。

这是一个我在哪里以及我想在Mvc中做什么的例子。

public class Student
{
    public int StudentID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }  
}

public class Course
{
    public int CourseID { get; set; }
    public string Course { get; set; }
    public string ClassRoom { get; set; }     
}

public class Course_Student
{
    public IEnumerable<Student> Students { get; set; }
    public IEnumerable<Course> Courses { get; set; }
}

在控制器的操作方法中:

Course_Student course_Student = new Course_Student();
course_Student.Courses = db.Courses; 
course_Student.Students = db.Students;
return View(course_Student);

我希望在视图中显示:

FirstName   LastName   Course    Room
Person      One        Math      C-110
Person      Two        Eng       D-123
Person      Three      Java      B-203
Person      Four       Speech    C-315

2 个答案:

答案 0 :(得分:0)

您的第一个问题是,您的ViewModel有点偏。

public class CourseViewModel
{
    public IEnumerable<Course_Student> PageData{ get; set; }
}

public class Course_Student
{
    public string FirstName { get; set; }
    public string LastName { get; set; } 
    public string Course { get; set; }
    public string ClassRoom { get; set; }        
}

修复了ViewModel,现在您的视图已清晰

@model CourseViewModel


<table>
 <th>FirstName</th>
 <th>LastName</th>
 ...etc
 @foreach(var cs in model.PageData){
  <tr><td>@cs.FirstName</td><td>@cs.LastName</td>...etc</tr>
 }
</table>

有了这个,您现在应该能够看到如何在控制器操作方法中填充ViewModel。您可以非常轻松地使用Linq来执行此操作。或者你可以为课程做一个for循环,并为每个课程创建一个结果Course_Student,并为你建立你的CoureViewModel.PageData列表,如果你需要有关该部分的帮助,请告诉我。

答案 1 :(得分:0)

您可以在Student课程中添加课程集合,以便所有课程都链接到Student课程(如果您愿意,可以反之亦然)。

public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int[] CourseIDs { get; set; }
}

public class Course
{
    public int CourseId { get; set; }
    public string CourseName { get; set; }
    public string ClassRoom { get; set; }
}

然后应简化您的视图模型,并将数据连接在一起,以便在视图中更轻松。

public class CourseStudent
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Course { get; set; }
    public string Room { get; set; }
}

public class CourseStudentViewModel
{
    public IEnumerable<CourseStudent> CourseStudents { get; set; }
}

如果您的Controller看起来像这样,那么您应该能够使它工作:

public ActionResult Index()
{
    var courses = new Collection<Course>
    {
        new Course {CourseId = 1, ClassRoom = "C-110", CourseName = "Math"},
        new Course {CourseId = 2, ClassRoom = "D-123", CourseName = "Eng"},
        new Course {CourseId = 3, ClassRoom = "B-203", CourseName = "Java"},
        new Course {CourseId = 4, ClassRoom = "C-315", CourseName = "Speech"},
    };

    var students = new Collection<Student>
    {
        new Student{StudentId = 1, FirstName = "Person", LastName = "One", CourseIDs = new []{1}},
        new Student{StudentId = 2, FirstName = "Person", LastName = "Two", CourseIDs = new []{2}},
        new Student{StudentId = 3, FirstName = "Person", LastName = "Three", CourseIDs = new []{3}},
        new Student{StudentId = 4, FirstName = "Person", LastName = "Four", CourseIDs = new []{4}},
    };

    var courseStudents = from student in students
        from course in courses
        where student.CourseIDs.Any(x => x.Equals(course.CourseId))
        select new CourseStudent
        {
            FirstName = student.FirstName,
            LastName = student.LastName,
            Course = course.CourseName,
            Room = course.ClassRoom
        };
    var courseStudentViewModel = new CourseStudentViewModel{CourseStudents = courseStudents};

    return View(courseStudentViewModel);
}

这样的观点:

@model MvcApplication1.CourseStudentViewModel
@{
    ViewBag.Title = "Index";
}

<table>
    <tbody><tr><th>FirstName</th><th>LastName</th><th>Course</th><th>Room</th></tr></tbody>
    <tbody>
        @foreach (var row in Model.CourseStudents)
        {
            <tr><td>@row.FirstName</td><td>@row.LastName</td><td>@row.Course</td><td>@row.Room</td></tr>
        }
    </tbody>
</table>

然后将结果作为html表:

Results as a html table