タプルは反復子を公開しません。
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);
}
次に、テーブルを構造化できます
<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>
これに慣れたら、階層ごとに継承と Entity Framework TPH テーブルを調べることができます。
次のような結果になる可能性があります。
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>