Transfer data from controller to view asp.net MVC

This asp.net MVC tutorial, we will discuss various asp.net MVC techniques to transfer data from controller to view.

ASP.NET MVC also provides state management techniques that can help us to maintain the data when redirecting from one page to another page or on the same page after reloading. There are several ways to do this in ASP.NET MVC.

HTTP is a stateless protocol, i.e., each HTTP request does not know about the previous request. If you are redirecting from one page to another page, then you have to maintain or persist your data so that you can access it further.

In Asp.Net MVC there are three ways to pass/store data between the controllers and views.

Also read few asp.net mvc tutorials:

Transfer data from controller to view asp.net MVC

ViewData:

  1. ViewData is used to pass data from controller to view
  2. It is derived from ViewDataDictionary class
  3. It is available for the current request only
  4. Requires typecasting for complex data type and checks for null values to avoid error
  5. If redirection occurs, then its value becomes null

ViewBag:

  1. ViewBag is also used to pass data from the controller to the respective view
  2. ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0
  3. It is also available for the current request only
  4. If redirection occurs, then its value becomes null
  5. Doesn’t require typecasting for complex data type

TempData:

  1. TempData is derived from TempDataDictionary class
  2. TempData is used to pass data from the current request to the next request
  3. It keeps the information for the time of an HTTP Request. This means only from one page to another. It helps to maintain the data when we move from one controller to another controller or from one action to another action
  4. It requires typecasting for complex data types and checks for null values to avoid an error. Generally, it is used to store only one time messages like the error messages and validation messages.

ViewData is nothing but a dictionary of objects and it is accessible by a string as a key. ViewData is a property of controller that exposes an instance of the ViewDataDictionary class.

ViewBag is very similar to ViewData. ViewBag is a dynamic property (dynamic keyword which is introduced in .net framework 4.0). ViewBag is able to set and get value dynamically and able to add any number of additional fields without converting it to strongly typed. ViewBag is just a wrapper around the ViewData.

ViewData Code:

Controller Code:

public ActionResult Index()
{
List<string> Student = new List<string>();
Student.Add("Satya1");
Student.Add("Satya11");
Student.Add("Satya111");
ViewData["Student"] = Student;
return View();
}

View code:

<ul>
<% foreach (var student in ViewData["Student"] as List<string>)
{ %>
<li><%: student%></li>
<% } %>
</ul>

ViewBag Code

Controller Code:

public ActionResult Index()
{
List<string> Student = new List<string>();
Student.Add("Satya1");
Student.Add("Satya11");
Student.Add("Satya111");
ViewBag.Student = Student;
return View();
}

View code:

<ul>
<% foreach (var student in ViewBag.Student)
{ %>
<li><%: student%></li>
<% } %>
</ul>

TempData is a dictionary which is derived from TempDataDictionary class. TempData is stored data just like live session for short time. TempData keeps data for the time of HTTP Request, which means that it holds data between two consecutive requests.

TempData helps us to transfer data between controllers or between actions. TempData internally uses Session variables. Note that TempData is only worked during the current and subsequent requests. It is generally used to store one time messages. With the help of the TempData.Keep() method we can keep value in the TempData object after request completion.

TempData Code:

Controller Code:

public ActionResult Index()
{
List<string> Student = new List<string>();
Student.Add("Satya1");
Student.Add("Satya11");
Student.Add("Satya111");
TempData["Student"] = Student;
return View();
}

View code:

<ul>
<% foreach (var student in TempData["Student"] as List<string>)
{ %>
<li><%: student%></li>
<% } %>
</ul>
ViewDataViewBagTempData
It is Key-Value Dictionary collectionIt is a type objectIt is Key-Value Dictionary collection
ViewData is a dictionary object and it is property of ControllerBase classViewBag is Dynamic property of ControllerBase class.TempData is a dictionary object and it is property of controllerBase class.
ViewData is Faster than ViewBagViewBag is slower than ViewDataNA
ViewData is introduced in MVC 1.0 and available in MVC 1.0 and aboveViewBag is introduced in MVC 3.0 and available in MVC 3.0 and aboveTempData is also introduced in MVC1.0 and available in MVC 1.0 and above.
ViewData also works with .net framework 3.5 and aboveViewBag only works with .net framework 4.0 and aboveTempData also works with .net framework 3.5 and above
Type Conversion code is required while enumeratingIn depth, ViewBag is used dynamic, so there is no need to type conversion while enumerating.Type Conversion code is required while enumerating
Its value becomes null if redirection has occurred.Same as ViewDataTempData is used to pass data between two consecutive requests.
It lies only during the current request.Same as ViewDataTempData only works during the current and subsequent request

You may like following asp.net tutorials:

Summary:

Here we had discussed about how to:

  1. ViewData, ViewBag, and TeampData for passing data from controller to view and in the next request.
  2. ViewData and ViewBag are almost similar and it helps us to transfer the data from controller to view.
  3. TempData also works during the current and subsequent requests.
Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>