Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

ASP.Net MVC - Multiple Models in Single View - Part1

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.

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 GetStudents()
{
return new List{ new Student() { StudentID = 1, StudentName = "Manish" },
new Student() { StudentID = 2, StudentName = "Prashant" },
new Student() { StudentID = 3, StudentName = "Deepak" }
};
}
public static List GetCourses()
{
return new List {
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" }
};
}
}

Output

Output of all approaches will be similar to the screenshot shown below:

Solutions to achieve

There are many ways of using multiple models in a view, most frequently used are given below:
  • Dynamic
  • ViewData
  • ViewBag
  • ViewModel
  • Tuple
  • ViewComponent(ASP.NET MVC 6 feature)

Let's see all the approaches one by one with example-

1. Dynamic

The ExpandoObject class enables us to add and delete members at run time.So If we want to build up an object to work with on the fly at runtime then we can use Expando Object.Let's see how can we use ExpandoObject with Multiple Models.

Action Method

public IActionResult UsingDynamic()
{
ViewBag.Message = "Passing Multiple Model using dynamic Model";
dynamic mymodel = new System.Dynamic.ExpandoObject();
mymodel.Students = Repository.GetStudents();
mymodel.Courses = Repository.GetCourses();
return View(mymodel);
}
In our View, we can define our model as dynamic (not a strongly typed model) using the @model dynamic keyword.

View Code

@using MVCCustomModelBinding
@model dynamic
@{
ViewBag.Title = "Home Page";
}

@ViewBag.Message



Student List








@foreach (var item in Model.Students)
{

}

Student IDStudent Name
@item.StudentID@item.StudentName



Course List








@foreach (var item in Model.Courses)
{

}

Course IDCourse Name
@item.CourseID@item.CourseName



2. View Data

ViewData is used to transfer data from the controller to the view. ViewData is a dictionary object that may be accessible using a string as the key. Using ViewData, we can pass any object from the controller to the view..View Data needs typecasting for getting data.
To use the ViewData for multiple models change your action method as below-

Action Method

public IActionResult UsingViewData()
{
ViewBag.Message = "Passing Multiple Model using ViewData";
ViewData["Students"] = Repository.GetStudents();
ViewData["Courses"] = Repository.GetCourses();
return View();
}

View Code

@using MVCCustomModelBinding.Models
@{
ViewBag.Title = "Home Page";
}

@ViewBag.Message



Student List








@foreach (var item in ViewData["Students"] as List)
{

}

Student IDStudent Name
@item.StudentID@item.StudentName



Course List








@foreach (var item in ViewData["Courses"] as List)
{

}

Course IDCourse Name
@item.CourseID@item.CourseName



3. ViewBag

ViewBag is similar to ViewData and is also used to transfer data from the controller to the view. ViewBag is a dynamic property. ViewBag is just a wrapper around the ViewData.

Action Method

public IActionResult UsingViewBag()
{
ViewBag.Message = "Passing Multiple Model using ViewBag";
ViewBag.Students = Repository.GetStudents();
ViewBag.Courses = Repository.GetCourses();
return View();
}

View Code

@using MVCCustomModelBinding.Models
@{
ViewBag.Title = "Home Page";
}

@ViewBag.Message



Student List








@foreach (var item in ViewBag.Students)
{

}

Student IDStudent Name
@item.StudentID@item.StudentName



Course List








@foreach (var item in ViewBag.Courses)
{

}

Course IDCourse Name
@item.CourseID@item.CourseName



To be continue....
In next post we will see how we can pass the multiple models using ViewModel,ViewComponent and Tuple.
I hope this will be helpful for you.


This post first appeared on Dot Net World, please read the originial post: here

Share the post

ASP.Net MVC - Multiple Models in Single View - Part1

×

Subscribe to Dot Net World

Get updates delivered right to your inbox!

Thank you for your subscription

×