Passing data from Controller to View in
ASP.NET MVC
ASP.NET MVC is a framework that facilitates building web applications based on MVC (Model-View-
Controller) design pattern. Request coming from client reaches the Controller through URL Rewriting
Module. Controller decides which model to use in order to fulfill the request. Further passing the
Model data to View which then transforms the Model data and renders response to client as shown
in following basic level request flow diagram.
Introduction
If we want to maintain state between a Controller and corresponding View- ViewData and
ViewBag are the available options but both of these options are limited to a single server call
(meaning it’s value will be null if a redirect occurs). But if we need to maintain state from
one Controller to another (redirect case), then TempData is the other available option.
It’s common that initially it might be a bit difficult for a ASP.NET WebForms developer to
digest above flow and need for options to pass data from Controller to View. Because in
WebForms approach, Controller and View are tightly coupled to each other. Please follow
the link for a detailed comparison of the differences between ASP.NET WebForms and
ASP.NET MVC here.
For the purpose of implementation, we will take earlier ASP.NET MVC tutorial on this blog
as base and implement with different options. If you haven’t gone through the article, please
read “Building your first ASP.NET MVC application in 4 simple steps” first.
ViewBag Example
As we discussed earlier that ViewBag and ViewData serves the same purpose but ViewBag is
basically a dynamic property (a new C# 4.0 feature) having advantage that it doesn’t have
typecasting and null checks.
So, In order to pass data from Controller to View using ViewBag, we will modify our
EmployeeController code as follows:
public class EmployeeController : Controller
{
// GET: /Employee/
public ActionResult Index()
{
ViewBag.EmployeeName = “Muhammad Hamza”;
ViewBag.Company = “Web Development Company”;
ViewBag.Address = “Dubai, United Arab Emirates”;
return View();
}
}
And to get Employee details passed from Controller using ViewBag, View code will be as
follows:
<body>
<div>
<h1>Employee (ViewBag Data Example)</h1>
<div>
<b>Employee Name:</b> @ViewBag.EmployeeName<br />
<b>Company Name:</b> @ViewBag.Company<br />
<b>Address:</b> @ViewBag.Address<br />
</div>
</div>
</body>
In order to see the above changes in action run the solution, we will find the following output.
ViewData Example
As compared to ViewBag, ViewData is a dictionary object which requires typecasting as well
as null checks. Same above implementation using ViewData can be achieved as follows:
public class EmployeeController : Controller
{
// GET: /Employee/
public ActionResult Index()
{
ViewData[“EmployeeName”] = “Muhammad Hamza”;
ViewData[“Company”] = “Web Development Company”;
ViewData[“Address”] = “Dubai, United Arab Emirates”;
return View();
}
}
And to get Employee details passed from Controller using ViewBag, View code will be as
follows:
<body>
<div>
<h1>Employee (ViewBag Data Example)</h1>
<div>
<b>Employee Name:</b> @ViewData[“EmployeeName”]<br />
<b>Company Name:</b> @ViewData[“Company”]<br />
<b>Address:</b> @ViewData[“Address”]<br />
</div>
</div>
</body>
Run the application to view the following output.
Using Model to pass data to View in
ASP.NET MVC
Exploring multiple ways to pass data from Controller to View in an ASP.NET MVC
application, so far we have covered following different approaches:
Part 1 – Using ViewData & ViewBag
Part 2 – Using TempData | TempData Vs Session
Now in this part, we are going to implement another approach i.e. using Model for passing
data from ASP.NET MVC Controller to View. As we already know that in MVC (Model,
View, Controller) pattern, Model represents our domain model corresponding to tables in
database. So, we can use this model class to serve the purpose.
Consider the following Model class we have already used in our previous implementations:
namespace MyMVCApp.Models
{
public class Employee
{
public string EmpID { get; set; }
public string EmpFirstName { get; set; }
public string EmpLastName { get; set; }
}
}
Now, we are done with our Model class (i.e. Employee). For the purpose of this
implementation, we are loading data directly to our Model in EmployeeController. In a real
implementation, we must be fetching this data from a data source e.g. a database. So, the
Controller has following code:
public class EmployeeController : Controller
{
public ActionResult Index()
{
List<Employee> employees = new List<Employee>()
{
new Employee{EmpID = “1”, EmpFirstName = “Imran”, EmpLastName =
“Ghani”},
new Employee{EmpID = “2”, EmpFirstName = “Rizwan”, EmpLastName =
“Mukhtar”},
new Employee{EmpID = “3”, EmpFirstName = “Rehan”, EmpLastName =
“Ahmad”},
new Employee{EmpID = “4”, EmpFirstName = “Zeeshan”, EmpLastName =
“Khalid”},
new Employee{EmpID = “5”, EmpFirstName = “Sajid”, EmpLastName =
“Majeed”}
};
return View(employees);
}
}
In above code example, you can see that as opposite to ViewData and ViewBag approach in
other ASP.NET MVC Tutorial, we are passing data (i.e. employees) as parameter to View
instead of placing in another store and calling as View().
Finally, updating our View to get and display data passed from Controller as:
@model IEnumerable<MyMVCApp.Models.Employee>
<html>
<head>
<meta name=”viewport” content=”width=device-width” />
<title>Employee Index Page</title>
</head>
<body>
<div>
<h1>Employee (Using Model)</h1>
<div>
@foreach(var employee in Model){
<p>@employee.EmpID @employee.EmpFirstName
@employee.EmpLastName</p>
}
</div>
</div>
</body>
</html>
At the top of our View (i.e. Index.cshtml), we have referred to our Model class (i.e.
Employee) using IEnumerable. Further in View, we are using foreach to access and display
data as needed. When we run this application, our View will display the Model data from
Controller as follows:
Using Model to pass data to View in ASP.NET is pretty simple as well as more appropriate
because we are fetching data from permanent store and loading in our Model classes in most
of the cases. Also, we use it to perform all CRUD (Create, Retrieve, Update, Delete)
operations. Hopefully, this ASP.NET MVC Tutorial will help in understanding the mentioned
approach in a more effective way.
Previous: ViewBag and ViewData in ASP.NET MVC