Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop through two viewbag items on View pages in MVC 4.0

Tags:

c#

asp.net-mvc

I want to display a table in the MVC 4.0 View page that has following code:

<table >
  <thead>           
    <tr>
      <th>Student Name</th>
      <th>Gaurdian</th>
      <th>Associate Teacher</th>

    </tr>
  </thead>

  @foreach(var stud in ViewBag.students)
  { 
     <tr>

         <td>@stud.Name</td>
     </tr>
   }

</table>

This works fine.But , the Guardian information and the associate teacher information is put in different ViewBag objects like ViewBag.guardians and Viewbag.assoc. How should i loop through them to display them in required table cells??

Loops within the loop like

   @foreach(var student in ViewBag.students)
  { 
    foreach(var gaurdian in ViewBag.guardians)
    {     
      <tr>

        <td>@student.Name</td>}
        <td>@guardian.Name</td>
      </tr>
    }
  }

sounds to be ridiculous. Please provide me proper solution. The student class contains guardian field but it has its own another class as follows:

      public class Student
      {
         public string Name {get;set;}
         public string RollNo {get;set;}
         public virtual Guardian Guardian {get;set;}
         public IList<Guardian> GuardianName {get;set;}
      }
       public class Guardian
      {
         public string Name{get;set;}
         public string MobNumber{get;set;}
       }  
      public class Associate
       {

          public string AID{get;set;}
          public virtual Student Student{get;set;}
          public string RollNo {get;set;}
       }            
like image 457
Bhushan Firake Avatar asked Nov 22 '12 08:11

Bhushan Firake


People also ask

Can we pass ViewBag from view to controller?

ViewBag itself cannot be used to send data from View to Controller and hence we need to make use of Form and Hidden Field in order to pass data from View to Controller in ASP.Net MVC Razor.

What is ViewData razor pages?

ViewData is a container for data to be passed from the PageModel to the content page. ViewData is a dictionary of objects with a string-based key. You add items to ViewData as follows: public class IndexModel : PageModel.

What is foreach loop in MVC?

Generally, the loops in asp.net mvc razor view will work same as other programming languages. We can define the loop inside or outside the code block in razor, and we can use the same foreach looping concept to assign value to define the condition.


2 Answers

You are doing this wrong, you should send a ienumerable of students as the views model.

then you can use student.Name, student.Guardian.Name

In you example you drop the relations between student and guardian

If you keep relations you can do

 @foreach(var student in ViewBag.students)
  {            
      <tr>    
        <td>@student.Name</td>
        <td>@student.Guardian.Name</td>
      </tr>        
  }

if you don't care about relations you can use a for loop

@for(int i=0; i < ViewBag.Students.Count(); i++)
{
   <tr>    
    <td>@ViewBag.Students[i].Name</td>
    <td>@ViewBag.Guardians[i].Name</td>
  </tr> 
}

Of course this only works as long as the students have a guardian or you will get stackoverflow ex :)

like image 112
jrb Avatar answered Oct 05 '22 02:10

jrb


Look at this What's the difference between ViewData and ViewBag?

It mean than you can loop through ViewBag items like this:

    @foreach (var viewBagItem in ViewContext.ViewData)
    {
        // your code
    }
like image 42
testCoder Avatar answered Oct 05 '22 01:10

testCoder