0% found this document useful (0 votes)
79 views

MVC Imp

The document discusses two approaches to displaying a list of teachers and students within a single view in ASP.NET MVC. The first approach uses an ExpandoObject as a dynamic model, adding the teacher and student lists as properties. The second approach defines a ViewModel class containing properties for the teacher and student lists. Both approaches retrieve sample teacher and student data and pass the model to the view for display.

Uploaded by

srinathms
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views

MVC Imp

The document discusses two approaches to displaying a list of teachers and students within a single view in ASP.NET MVC. The first approach uses an ExpandoObject as a dynamic model, adding the teacher and student lists as properties. The second approach defines a ViewModel class containing properties for the teacher and student lists. Both approaches retrieve sample teacher and student data and pass the model to the view for display.

Uploaded by

srinathms
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Teacher and Student, and I need to display a list of teachers and students within a single view.

The following are the model definitions for the Teacher and Student classes.

public class Teacher


{
public int TeacherId { get; set; }
public string Code { get; set; }
public string Name { get; set; }
}

public class Student


{
public int StudentId { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public string EnrollmentNo { get; set; }
}

The following are the methods that help us to get all the teachers and students.

private List<Teacher> GetTeachers()


{
List<Teacher> teachers = new List<Teacher>();
teachers.Add(new Teacher { TeacherId = 1, Code = "TT", Name = "Tejas Trivedi" });
teachers.Add(new Teacher { TeacherId = 2, Code = "JT", Name = "Jignesh Trivedi" });
teachers.Add(new Teacher { TeacherId = 3, Code = "RT", Name = "Rakesh Trivedi" });
return teachers;
}

public List<Student> GetStudents()


{
List<Student> students = new List<Student>();
students.Add(new Student { StudentId = 1, Code = "L0001", Name = "Amit Gupta", EnrollmentNo
="201404150001" });
students.Add(new Student { StudentId = 2, Code = "L0002", Name = "Chetan Gujjar", EnrollmentNo
="201404150002" });
students.Add(new Student { StudentId = 3, Code = "L0003", Name = "Bhavin Patel", EnrollmentNo
="201404150003" });
return students;
}

There are many ways to use multiple models with a single view. Here I will explain ways one by one.

1. Using Dynamic Model


ExpandoObject (the System.Dynamic namespace) is a class that was added to the .Net Framework 4.0 that
allows us to dynamically add and remove properties onto an object at runtime. Using this ExpandoObject,
we can create a new object and can add our list of teachers and students into it as a property. We can
pass this dynamically created object to the view and render list of the teacher and student.

Controller Code

public class HomeController : Controller


{
public ActionResult Index()
{
ViewBag.Message = "Welcome to my demo!";
dynamic mymodel = new ExpandoObject();
mymodel.Teachers = GetTeachers();
mymodel.Students = GetStudents();
return View(mymodel);
}
}

We can define our model as dynamic (not a strongly typed model) using the @model dynamic keyword.

View Code

@using MultipleModelInOneView;
@model dynamic
@{
ViewBag.Title = "Home Page";
}
<h2>@ViewBag.Message</h2>

<p><b>Teacher List</b></p>

<table>
<tr>
<th>Id</th>
<th>Code</th>
<th>Name</th>
</tr>
@foreach (Teacher teacher in Model.Teachers)
{
<tr>
<td>@teacher.TeacherId</td>
<td>@teacher.Code</td>
<td>@teacher.Name</td>
</tr>
}
</table>
<p><b>Student List</b></p>

<table>
<tr>
<th>Id</th>
<th>Code</th>
<th>Name</th>
<th>Enrollment No</th>
</tr>
@foreach (Student student in Model.Students)
{
<tr>
<td>@student.StudentId</td>
<td>@student.Code</td>
<td>@student.Name</td>
<td>@student.EnrollmentNo</td>
</tr>
}
</table>

2. Using View Model

ViewModel is nothing but a single class that may have multiple models. It contains multiple models as a
property. It should not contain any method.

In the above example, we have the required View model with two properties. This ViewModel is passed to
the view as a model. To get intellisense in the view, we need to define a strongly typed view.

public class ViewModel


{
public IEnumerable<Teacher> Teachers { get; set; }
public IEnumerable<Student> Students { get; set; }
}

Controller code

public ActionResult IndexViewModel()


{
ViewBag.Message = "Welcome to my demo!";
ViewModel mymodel = new ViewModel();
mymodel.Teachers = GetTeachers();
mymodel.Students = GetStudents();
return View(mymodel);
}
View code

@using MultipleModelInOneView;
@model ViewModel
@{
ViewBag.Title = "Home Page";
}
<h2>@ViewBag.Message</h2>

<p><b>Teacher List</b></p>

<table>
<tr>
<th>Id</th>
<th>Code</th>
<th>Name</th>
</tr>
@foreach (Teacher teacher in Model.Teachers)
{
<tr>
<td>@teacher.TeacherId</td>
<td>@teacher.Code</td>
<td>@teacher.Name</td>
</tr>
}
</table>

<p><b>Student List</b></p>

<table>
<tr>
<th>Id</th>
<th>Code</th>
<th>Name</th>
<th>Enrollment No</th>
</tr>
@foreach (Student student in Model.Students)
{
<tr>
<td>@student.StudentId</td>
<td>@student.Code</td>
<td>@student.Name</td>
<td>@student.EnrollmentNo</td>
</tr>
}
</table>

You might also like