Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8886159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:29:24+00:00 2026-06-14T21:29:24+00:00

I have a class student: public class Student { public int StudentId{get;set;} public string

  • 0

I have a class student:

       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;}
       }

And the Course class is:

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

The Staff class has the following structure:

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

I have put the data in the ViewBag , but I am not able figuring it out how should I use foreach statement to print all those records in the following View.
View:

      <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>

But, this prints courses taken by all the students for the single student as they are in the first foreach loop. Please , suggest me a way to do this…!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:29:26+00:00Added an answer on June 14, 2026 at 9:29 pm

    You don’t need your other viewbags. You have references to your courses and staff in the student object.

    <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>
    

    You may also want to consider making IEnumerable the model for your page by adding:

    @model IEnumerable<myproject.Models.Student>
    

    to the top of the page. Then you will not need to use a Viewbag.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have two classes: public class Student { public int Id {get; set;}
i have these models: public class Student { public int Id { get; set;
Say i have a object like this public class Student{ public IList<Coursework> Courseworks{get;set;} public
I have a class lets say students with properties as public string sname{get;set;} public
I have a Student object: public class Student { public int Id { get;
Given the classes: public class Person { public string Name { get; set; }
Class Student { String Name {get; set} //Extended property of the student School Shooling
I have a class named Student public class Student { public string Name {
I have a Student class in my application as below: public class Student {
I have a simple class Student under namespace School. namespace XmlTestApp { public class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.