How to display multiple Model classes in a single view using ViewModel in ASP.NET MVC 4 ?


In this article , I am going to explain on displaying multiple Model classes in a single view using ViewModel in ASP.NET MVC 4 . ViewModel is a pattern that allow us to have multiple models as a single class. It contains properties of entities exactly need to be used in a view. ViewModel should not have methods. It should be a collection of properties needed for a view.

Create two classes Student.cs and Class.cs in Models folder.

Add below code to Student.cs


using System.ComponentModel.DataAnnotations;
public class Student
{
[Key]
public int Id { get; set; }
public string SName { get; set; }

}

similarly add below code to Class.cs
 
using System.ComponentModel.DataAnnotations;

public class Class
{
[Key]
public int Id { get; set; }
public string CName { get; set; }

}

Now create another class called Repository.cs in Models folder.This class used to add data to Student.cs and Class.cs classes.Below code used in Repository.cs class.

 
public List GetStudents()
{
return new List {
new Student () { Id = 1, SName = "Chemistry"},
new Student () { Id = 2, SName = "Physics"},
new Student () { Id = 3, SName = "Math" },
new Student () { Id = 4, SName = "Computer Science" }
};
}

public List GetClasses()
{
return new List {
new Class () { Id = 1, CName = "ECE"},
new Class () { Id = 2, CName = "EEE"},
new Class () { Id = 3, CName = "CSE" },
new Class () { Id = 4, CName = "IT" }
};
}

Now create ViewModel class in Models folder.SCViewModel is the ViewModel class name.

 
public class SCViewModel
{
public List SDetails { get; set; }
public List CDetails { get; set; }
}

Now in Models folder we have added 4 classes i.e
Student.cs,
Class.cs,
Repository.cs,
SCViewModel.cs

Now let us go to controller.Add a controller with Controller Name as HelloWorld .Then by default we get Index Action Method.Add view to this Index() method.Let us add another method to this HelloWorld controller called ViewModelDemo() .
 
Repository _repository = new Repository();
public ActionResult ViewModelDemo()
{
SCViewModel vm = new SCViewModel();
vm.SDetails = _repository.GetStudents();
vm.CDetails = _repository.GetClasses();


return View(vm);
}


Now add view to this method ViewModelDemo() .In ViewModelDemo.cshtml view, add below code.

@model WebApplication3.Models.SCViewModel
@{
ViewBag.Title = "ViewModelDemo";
}

< h2 >Calling ViewModelDemo method in HelloWorld Controller to display multiple Model classes in a single View < / h2>
< p style="color:green">Student Details< /p>
<table cellpadding="2" border="2" >
<tr>
<th> Student Id </th>
<th> Student Name </th>
</tr>




@*Iterating Course ViewModel *@
@foreach (var item in Model.SDetails)
{
<tr> <td>@item.Id </td> <td> @item.SName </td> </tr>



}
</table>

< p style="color:green">Class Details< /p>

<table cellpadding="5" border="5">
<tr>
<th> Class Id </th>
<th> Class Name </th>
</tr>


@foreach (var item in Model.CDetails)
{
<tr><td> @item.Id</td>
<td> @item.CName </td></tr>

}

</table>

Thus,by using above code we can easily display multiple model classes in a single view using ViewModel .


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: