?
Solved

ASP.Net MVC3 Multiple models on a single view

Posted on 2013-06-03
3
Medium Priority
?
298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 1500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

752 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