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
Solved

ASP.Net MVC3 Multiple models on a single view

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

808 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