2013-06-27 1 views
0

두 개의 클래스 - Student.cs 및 Lecturer.cs가 모델 아래에 있습니다. 이제 내 면도보기에서 두 클래스를 함께 배치해야합니다.보기의 두 모델 (foreach 포함)

나는이 문제를 해결하기위한 튜플 방법을 알고있다. 그러나 나는 다음에해야할 일을 모른다. @foreach로 무엇을해야합니까?

여기 내 코드는 cshtml입니다.

@model Tuple<MVCApp1.Models.Student, MVCApp1.Models.Lecturer> 

@{ 
    ViewBag.Title = "MainPage"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

다음 표는 @foreach 코드 섹션입니다.

@foreach (var j in Model) 
      { 
       <tr> 
        <td">@j.FirstName 
        </td> 
        <td>@j.MiddleName 
        </td> 
        <td>@j.LastName 
        </td> 

각각 다른 속성을 가진 2 개의 테이블이 있어야합니다. Student.cs의 첫 번째 테이블과 두 번째 테이블은 Lecturer.cs입니다. @foreach에 문제가 있음을 알고 있지만 온라인에서는 해결책을 찾을 수 없습니다. 도와주세요.

답변

0

튜플은 반복기를 노출하지 않습니다.

public class Tuple<T1> : IStructuralEquatable, IStructuralComparable, IComparable, ITuple 

뒤에 오는 것은 ViewModel입니다.

public class ViewModel 
{ 
    public List<Student> Students { get; set; } 
    public List<Teacher> Teachers { get; set; } 
} 

public ActionResult Index() 
{ 
    ViewModel model = new ViewModel(); 

    // retreive from database 
    model.Students = new List<Student>() { new Student()}; 
    model.Teachers = new List<Teacher>() { new Teacher()}; 

    return View(model); 
} 

그럼 당신이 익숙해지면, 당신은 계층 구조에 따라 상속과 엔티티 프레임 워크 TPH 표를 탐색 할 수 있습니다 테이블

<table> 
    <tr> 
     <th>First</th> 
     <th>Middle</th> 
     <th>Last</th> 
    </tr> 
    @foreach (var student in Model.Students) 
    { 
     <tr> 
      <td>@student.First</td> 
      <td>@student.Middle</td> 
      <td>@student.Last</td> 
     </tr> 
    } 
    @foreach (var teacher in Model.Teachers) 
    { 
     <tr> 
      <td>@teacher.First</td> 
      <td>@teacher.Middle</td> 
      <td>@teacher.Last</td> 
     </tr> 
    } 
</table> 

을 구성 할 수 있습니다.

이 같은 끝낼 수 :

public abstract class Person 
{ 
    public int Id { get; set; } 
    public string First { get; set; } 
    public string Middle { get; set; } 
    public string Last { get; set; } 
} 

public class Teacher : Person 
{ 
    public string Class { get; set; } 
    public DateTime HireDate { get; set; } 
} 

public class Student : Person 
{ 
    public int Grade { get; set; } 
    public DateTime EnrolledDate { get; set; } 
} 

public class ViewModel 
{ 
    public List<Student> StudentsOnly { get; set; } 
    public List<Person> StudentsAndTeachers { get; set; } 
} 

public ActionResult Index() 
{ 
    Context db = new Context(); 

    ViewModel model = new ViewModel(); 
    // You could collect just the students 
    model.StudentsOnly = db.People.OfType<Student>().ToList(); 
    // Or all of them 
    model.StudentsAndTeachers = db.People.ToList(); 

    return View(model); 
} 

그런 다음 당신은 단지 당신은 단지 자신의 이름을 표시하는 데 필요한 경우, 사람들의 단일 목록을 반복해야합니다.

<table> 
    ... 
    @foreach (var person in Model.StudentsAndTeachers) 
    { 
     <tr> 
      <td>@person.First</td> 
      <td>@person.Middle</td> 
      <td>@person.Last</td> 
     </tr> 
    } 
</table> 
+0

프로젝트에서 '/ ViewModels/ViewModel.cs' 또는 새 페이지의 특정 이름을 지정하여 새 폴더를 만들 수 있습니다. 테이블 일 때,'ListingViewModel.cs' –

+1

'new List () {new Student()};가 여기에 채워지고 평소처럼 ViewModel에 할당하는 것이 어떻습니까? –

+0

대단히 감사합니다! :) – sniggy