0

私にはクラスの生徒がいます:

       public class Student
       {
          public int StudentId{get;set;}
          public string StudentName{get;set;}
          public int Age{get;set;}
          public List<Course> Courses{get;set;}
          public List<string> MobileNumbers{get;set;}
       }

そして、コースクラスは次のとおりです。

       public class Course
       {
         public int CourseId {get;set;}
         public string CourseName {get;set;}
         public List<Staff> Staff {get;set;}
       }

スタッフクラスの構造は次のとおりです。

      public class Staff
      {
        public int StaffId {get;set;}
        public string StaffName {get;set;}
      }

データをViewBagに入れましたが、foreachステートメントを使用してこれらすべてのレコードを次のビューに出力する方法がわかりません。意見:

      <table class="tableStyle" width="100%" border="0" cellspacing="0" cellpadding="0">

                        <thead>
                            <tr>
                                <th>Student Name</th>
                                <th>Courses</th>
                                <th>Staffs</th>
                                <th>Mobile Numbers</th>
                            </tr>
                        </thead>

                        @foreach (Student stud in ViewBag.students)
                        {       
                            <tr>
                                <td>@stud.StudentName</td>
                              foreach(Course course in ViewBag.courses)
                              {  
                                <td>@course.CourseName</td>

                              }
                              foreach(Staff staff in ViewBag.staff)
                              {
                                    <td>@staff.StaffName</td>
                              }

                            </tr>
                       }
                    </table>

ただし、これは、最初のforeachループにあるため、すべての学生が1人の学生のために受講したコースを印刷します。どうか、これを行う方法を教えてください... !!

4

1 に答える 1

1

他のビューバッグは必要ありません。学生オブジェクトにコースとスタッフへの参照があります。

<table class="tableStyle" width="100%" border="0" cellspacing="0" cellpadding="0">

                        <thead>
                            <tr>
                                <th>Student Name</th>
                                <th>Courses - Staffs</th>
                                <th>Mobile Numbers</th>
                            </tr>
                        </thead>

                        @foreach (Student stud in Viewbag.students)
                        {       
                            <tr>
                                <td>@stud.StudentName</td>
                                <td>
                              foreach(Course course in stud.courses)
                              {  
                                foreach(Staff staff in course.staff)
                                {
                                  @course.CourseName - @staff.StaffName </br>
                                }
                              }
                                </td>

                            </tr>
                       }
                    </table>

次を追加して、IEnumerableをページのモデルにすることを検討することもできます。

@model IEnumerable<myproject.Models.Student>

ページの上部に。そうすれば、Viewbagを使用する必要はありません。

于 2012-11-26T16:11:10.250 に答える