Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 300
  • Last Modified:

ASP.Net MVC3 Multiple models on a single view

I have a view with tabs.  Suppose there are two tabs, one that has a grid for all students and one with a grid for all Teachers.  The view needs to be able to provide a single model with many sub models, which drive the grids.  I’ve taken shotcuts on the code in order to illustrate my situation.

My problem is wrapping my head around how to structure the class so it contains the two models “Teacher” and “Student”.

I’ve come across posts where it’s suggested that you create a wrapper class but I’m not sure how to do this.  I need an example on how to do this.



Within my view I need to be able to do something like the following:

@Model IEnumerable<schoolManagement>

Foreach (var item in schoolManagement.Get_Teacher_Data)
{
      Teachergrid grid1 = new grid(model.Get_Teacher_Data, “Selectedrow”)
}

Foreach (var item in schoolManagement.Get_Student_Data)
{
      Teachergrid grid2 = new grid(model.Get_Student_Data, “Selectedrow”)
}




The Model Class:


public class SchoolManagement
    {
       
 public List<Qry_Teachers> Get_Teacher_Data(int projNo)
        {
            string sqlViewName = "vw_Get_Teacher_Select";
            SQLDataAccessLayer DAL = new SQLDataAccessLayer();

            try
            {
                //load the qry object with data
                return DAL.Get_Teacher_Data(sqlViewName, projNo);
            }

            catch (Exception xptn)
            {
                throw;
            }
        }

      public List<Qry_Students> Get_Student_Data(int projNo)
        {
            string sqlViewName = "vw_Get_Student_Select";
            SQLDataAccessLayer DAL = new SQLDataAccessLayer();

            try
            {
                //load the qry object with data
                return DAL.Get_Student_Data(sqlViewName, projNo);
            }

            catch (Exception xptn)
            {
                throw;
            }
        }
    }
}


The controller:

Public ActionResult Index()
{

return View(SchoolManagement());

}
0
Steve7423
Asked:
Steve7423
  • 2
1 Solution
 
Craig WagnerSoftware ArchitectCommented:
Your SchoolManagement class is not a Model, it's a Data Access Layer. A Model is just a class with properties, not a class that does the actual retrieval. The retrieval should happen in the Controller, not in the View.

What you should have is something like this:

public class Student
{
    public string Name {get; set;}
    // other properties for a student
}

public class Teacher
{
    public string Name {get; set;}
    // other properties for a teacher
}

public class SchoolManagement
{
    public IEnumerable<Student> Students {get; set;}
    public IEnumerable<Teacher> Teachers {get; set;}
}

Open in new window

0
 
Steve7423Author Commented:
K.  I'll make some changes using your suggestion.  Give me a day or so.

Thanks
0
 
Steve7423Author Commented:
This worked, thanks
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now