Solved

ASP.Net MVC3 Multiple models on a single view

Posted on 2013-06-03
3
293 Views
Last Modified: 2013-07-18
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
Comment
Question by:Steve7423
  • 2
3 Comments
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 500 total points
ID: 39218930
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
 

Author Comment

by:Steve7423
ID: 39219070
K.  I'll make some changes using your suggestion.  Give me a day or so.

Thanks
0
 

Author Closing Comment

by:Steve7423
ID: 39337805
This worked, thanks
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question