In MVC we cannot use multiple model tag on a view. But Many times we need to pass multiple models from controller to view or we want to show data from multiple model on a view. So In this post we will see the different ways to bind or pass the multiple models to single view.
Below are the model definition for Student and Course
Below is the Repository class which have two method for returning the list of Students and Courses
Problem Statement
Lets suppose we have two models Student and Course and we need to show the the list of Students and Courses in a single view.Below are the model definition for Student and Course
public class Student { public int StudentID { get; set; } public string StudentName { get; set; } } public class Course { public int CourseID { get; set; } public string CourseName { get; set; } }
Below is the Repository class which have two method for returning the list of Students and Courses
public class Repository { public static List<student> GetStudents() { return new List<student>{ new Student() { StudentID = 1, StudentName = "Manish" }, new Student() { StudentID = 2, StudentName = "Prashant" }, new Student() { StudentID = 3, StudentName = "Deepak" } }; } public static List<course> GetCourses() { return new List<course> { new Course () { CourseID = 1, CourseName = "Chemistry"}, new Course () { CourseID = 2, CourseName = "Physics"}, new Course () { CourseID = 3, CourseName = "Math" }, new Course () { CourseID = 4, CourseName = "Computer Science" } }; } }