وضعیت View نیز به شکل زیر خواهد بود.
<table>
<tr>
<th>Id</th>
<th>Code</th>
<th>Name</th>
</tr>
@foreach (Teacher teacher in 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 students)
{
<tr>
<td>@student.StudentId</td>
<td>@student.Code</td>
<td>@student.Name</td>
<td>@student.EnrollmentNo</td>
</tr>
}
</table>
4- روش استفاده از Viewbag
شکل Controller به شکل زیر خواهد بود.
- public ActionResult IndexViewBag()
- {
- ViewBag.Message = "Welcome to my demo!";
- ViewBag.Teachers = GetTeachers();
- ViewBag.Students = GetStudents();
- return View();
- }
و Viewباید مطابق شکل زیر باشد.
@using MultipleModelInOneView;
@{
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 ViewBag.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 ViewBag.Students)
{
<tr>
<td>@student.StudentId</td>
<td>@student.Code</td>
<td>@student.Name</td>
<td>@student.EnrollmentNo</td>
</tr>
}
</table>
5- روش استفاده از Tuple
این روش تا حدودی جدید است و استفاده از آن بسیار زیبا و بر اساس تکنولوژی جدید C# خواهد بود.
شکل Controller و Action Method لازمه به شکل زیر خواهد بود.
- public ActionResult IndexTuple(){
- ViewBag.Message = "Welcome to my demo!";
- var tupleModel = new Tuple<List<Teacher>, List<Student>>(GetTeachers(), GetStudents());
- return View(tupleModel);}
حال وضعیت View به شکل زیر خواهد بود.
@using MultipleModelInOneView;
@model Tuple < List<Teacher>, List <Student>>
@{ 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.Item1)
{
<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.Item2)
{
<tr>
<td>@student.StudentId</td>
<td>@student.Code</td>
<td>@student.Name</td>
<td>@student.EnrollmentNo</td>
</tr>
}
</table>
6- استفاده از RenderAction و PartialView
در این روش وضعیت Controller به شکل زیر خواهد بود.
- public ActionResult PartialView()
- {
- ViewBag.Message = "Welcome to my demo!";
- return View();
- }
-
-
-
-
-
- public PartialViewResult RenderTeacher()
- {
- return PartialView(GetTeachers());
- }
-
-
-
-
-
- public PartialViewResult RenderStudent()
- {
- return PartialView(GetStudents());
- }
در Index View وضعیتی مشابه زیر خواهیم داشت.
@{ ViewBag.Title = "PartialView";
<h2>@ViewBag.Message</h2>
<div>
@{ Html.RenderAction("RenderTeacher");
Html.RenderAction("RenderStudent"); }
</div>
وضعیت PartialView ها به شکل زیر خواهد بود.
RenderTeacher.cshtml
@using MultipleModelInOneView;@model IEnumerable
<MultipleModelInOneView.Teacher>
<p><b>Teacher List</b></p>
<table>
<tr>
<th>Id</th>
<th>Code</th>
<th>Name</th>
</tr>
@foreach (Teacher teacher in Model)
{
<tr>
<td>@teacher.TeacherId</td>
<td>@teacher.Code</td>
<td>@teacher.Name</td>
</tr>
}
</table>
RenderStudent.cshtml
@using MultipleModelInOneView;@model IEnumerable
<MultipleModelInOneView.Student>
<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)
{
<tr>
<td>@student.StudentId</td>
<td>@student.Code</td>
<td>@student.Name</td>
<td>@student.EnrollmentNo</td>
</tr>
}
</table>
در Controller دو Action Method سبب صدا زده شدن و مقدار گرفتن و نمایش دو PartialView ایجاد شده خواهد گردید.