Solved

ASP.Net MVC3 Multiple models on a single view

Posted on 2013-06-03
3
291 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now