如何通过在MVC 4.0浏览网页的两种viewbag项目环 [英] How to loop through two viewbag items on View pages in MVC 4.0

查看:112
本文介绍了如何通过在MVC 4.0浏览网页的两种viewbag项目环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在MVC 4.0查看页面中显示的表中有以下code:

 <表>
  <&THEAD GT;
    &所述; TR>
      <第i个学生姓名< /第i
      <第i Gaurdian< /第i
      <第i个准教师和LT; /第i    < / TR>
  < / THEAD>  @foreach(在ViewBag.students VAR螺栓)
  {
     &所述; TR>         < TD> @ stud.Name< / TD>
     < / TR>
   }< /表>

这工作fine.But,监护人信息和联系教师信息放在像ViewBag.guardians和Viewbag.assoc不同ViewBag对象。我应该如何通过它们循环所需表格单元格中显示出来??

内环路

环路像

  @foreach(VAR学生ViewBag.students)
  {
    的foreach(在ViewBag.guardians VAR gaurdian)
    {
      &所述; TR>        < TD> @ student.Name< / TD>}
        < TD> @ guardian.Name< / TD>
      < / TR>
    }
  }

听起来是荒谬的。请给我提供妥善的解决方案。
学生类包含监护人的领域,但它有自己的另一个类,如下所示:

 公共类学生
      {
         公共字符串名称{;设置;}
         公共字符串RollNo {获取;设置;}
         公共虚拟监护人监护人{获取;设置;}
         公众的IList<&卫士GT; GuardianName {获取;设置;}
      }
       公共类卫士
      {
         公共字符串名称{;设置;}
         公共字符串MobNumber {获取;设置;}
       }
      公共类关联
       {          公共字符串AID {获取;设置;}
          公共虚拟学生学生{获取;设置;}
          公共字符串RollNo {获取;设置;}
       }


解决方案

您这样做是不对的,你应该送一个IEnumerable的学生为视窗模式。

然后就可以使用student.Name,student.Guardian.Name

在你比如你砸的学生和监护人之间的关系。

如果你保持关系,你可以做

  @foreach(VAR学生ViewBag.students)
  {
      &所述; TR>
        < TD> @ student.Name< / TD>
        < TD> @ student.Guardian.Name< / TD>
      < / TR>
  }

如果你不关心的关系,您可以使用一个for循环

  @for(INT I = 0; I< ViewBag.Students.Count();我++)
{
   &所述; TR>
    < TD> @ ViewBag.Students [I] .Name点< / TD>
    < TD> @ ViewBag.Guardians [I] .Name点< / TD>
  < / TR>
}

当然,这只是工作,只要学生有监护人或你会得到计算器例如:)

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

解决方案

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

这篇关于如何通过在MVC 4.0浏览网页的两种viewbag项目环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆