Hi friends,in this article I will explain about How to show Multiple
Models in a Single View using dynamically created object.
I already explained in the previous articles about Enhancing WebGrid with Insert Update and Delete Operations Using Repository Pattern with Entity Framework in ASP.NET MVC 4 Razor, How to Update multiple rows at once Using MVC 4 Razor and Entity Framework and MVC 4 Razor: Delete multiple rows in WebGrid with CheckBox selection and with confirmation in ASP.NET using C#.NET
I already explained in the previous articles about Enhancing WebGrid with Insert Update and Delete Operations Using Repository Pattern with Entity Framework in ASP.NET MVC 4 Razor, How to Update multiple rows at once Using MVC 4 Razor and Entity Framework and MVC 4 Razor: Delete multiple rows in WebGrid with CheckBox selection and with confirmation in ASP.NET using C#.NET
Suppose
I have two models, Course and Student, and I need to display a list of courses
and students within a single view. How can we do this? Below is the
solution
using System;
using System.Collections;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
namespace
MVCMultipleModelsinView.Models
{
public
class Student
{
public int
studentID { get; set;
}
public string
studentName { get; set;
}
public string
EnrollmentNo { get; set;
}
public string
courseName { get; set;
}
public List<Student> GetStudents()
{
List<Student>
students = new List<Student>();
students.Add(new Student {
studentID = 1, studentName = "Kishore",
EnrollmentNo = "K0001",
courseName =".NET"});
students.Add(new Student {
studentID = 2, studentName = "Satya",
EnrollmentNo = "K0002",
courseName = "MVC" });
students.Add(new Student {
studentID = 3, studentName = "Raghav",
EnrollmentNo = "K0003",
courseName = "Silverlight" });
return students;
}
}
}
|
Model(Course.cs):
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
namespace
MVCMultipleModelsinView.Models
{
public
class Course
{
public int courseID
{ get; set; }
public string
courseCode { get; set;
}
public string
courseName { get; set;
}
public List<Course> GetCourses()
{
List<Course>
Courses = new List<Course>();
Courses.Add(new Course {
courseID = 1, courseCode = "CNET",
courseName = ".NET" });
Courses.Add(new Course {
courseID = 2, courseCode = "CMVC",
courseName = "MVC" });
Courses.Add(new Course {
courseID = 3, courseCode = "CSVR",
courseName = "Silverlight" });
return Courses;
}
}
}
|
Controller (CourseStudentController.cs):
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Dynamic;
using
MVCMultipleModelsinView.Models;
namespace
MVCMultipleModelsinView.Controllers
{
public
class CourseStudentController
: Controller
{
public ActionResult
Index()
{
ViewBag.Message = "Welcome!";
dynamic model = new
ExpandoObject();
Student stu = new
Student();
model.students =
stu.GetStudents();
Course crs = new
Course();
model.courses =
crs.GetCourses();
return View(model);
}
}
}
|
View(Index.cshtml):
@using
MVCMultipleModelsinView.Models;
@{
ViewBag.Title = "Index";
}
<h2>@ViewBag.Message</h2>
<style type="text/css">
table
{
margin: 4px;
border-collapse: collapse;
width: 500px;
font-family:Tahoma;
}
th
{
background-color: #990000;
font-weight: bold;
color: White !important;
}
table
th a
{
color: White;
text-decoration: none;
}
table
th, table
td
{
border: 1px solid black;
padding: 5px;
}
</style>
<p>
<b>Student List</b></p>
<table>
<tr>
<th>
Student Id
</th>
<th>
Student Name
</th>
<th>
Course Name
</th>
<th>
Enrollment No
</th>
</tr>
@foreach
(Student stu in
Model.students)
{
<tr>
<td>@stu.studentID
</td>
<td>@stu.studentName
</td>
<td>@stu.courseName
</td>
<td>@stu.EnrollmentNo
</td>
</tr>
}
</table>
<p>
<b>Course List</b></p>
<table>
<tr>
<th>
Course Id
</th>
<th>
Course Code
</th>
<th>
Course Name
</th>
</tr>
@foreach
(Course crs in
Model.courses)
{
<tr>
<td>@crs.courseID
</td>
<td>@crs.courseCode
</td>
<td>@crs.courseName
</tr>
}
</table>
|
You can download the code by clicking on the below Download image.
Nice Blog,
ReplyDeleteThanks for the piece of code. I was searching for this, You were helpful.
eCommerce Solution Provider India