In this article, you will learn about how to pass data from one page to other page. You can understand it better when I will use ASP.NET MVC terms here. Yes, that is right. I am talking about passing the data from controller to view, controller to other controller and controller to action.
As we know in ASP.NET, we used lots of way to pass the data from one page to other page or store the data and access it on another page. These are Session, ViewState, QueryString, Cross Page Posting, etc. So, these are the ways in ASP.NET, but what about the ways ASP.NET MVC.
ASP.NET MVC provides us more flexibility compared to ASP.NET. As we know that ASP.NET MVC is pure html base, so here we use three ASP.NET MVC objects for passing the data or we can say transferring the data.
The following are the object which store the value and transfer it from one place to other place.
- ViewData
- ViewBag
- TempData

Create an empty ASP.NET MVC application to understand the ViewData, ViewBag and TempData with example step by step. Open Visual Studio and choose File Menu, then New and click on Project.
It will give you a New Project dialog window where you need to choose the ASP.NET Web Application and provide the name of the application and click on OK.

It will give you one more dialog to add the MVC application with some specific template. You need to choose the MVC and click on OK.

Add a model class “Student.cs” with the following properties.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace PassingDataInMVC.Models
- {
- public class Student
- {
- public int StudentId { get; set; }
- public string StudentName { get; set; }
- public string Address { get; set; }
- }
- }

ViewData
It was introduced with MVC 1.0 and MVC 2.0. It is basically a dictionary object and stores the data with object type. It is derived from ViewDataDictionary. The data for ViewData exists only for current request. As soon as the corresponding view is going to render on browser, the ViewData is emptied.
It is used to transfer the data from controller to its corresponding view. It uses typecasting for getting the data and also you can check the data for null.
The following are the syntax to pass the data using ViewData.
- ViewData[“name”]=value;
- ViewContext.ViewData[“name”]=value;
- ViewContext.Controller.ViewData[“name”]=value;
<h2> my value is: @ViewData[“name”]</h2>
Controller
- @using PassingDataInMVC.Models
- @{
- ViewBag.Title = "Index";
- }
- <h2>Index</h2>
- <div>
- <h4>Student</h4>
- <hr />
- <dl class="dl-horizontal">
- @{
- var studentDetail = ViewData["StudentDetail"] as Student;
- }
- </dl>
- <h2> @ViewData["Message"] </h2>
- <br />
- <p>Sudent Id : @studentDetail.StudentId</p>
- <p>Sudent Name : @studentDetail.StudentName</p>
- </div>
- @using PassingDataInMVC.Models
- @{
- ViewBag.Title = "Index";
- }
- <h2>Index</h2>
- <div>
- <h4>Student</h4>
- <hr />
- <dl class="dl-horizontal">
- @{
- var studentDetail = ViewData["StudentDetail"] as Student;
- }
- </dl>
- <h2> @ViewData["Message"] </h2>
- <br />
- <p>Sudent Id : @studentDetail.StudentId</p>
- <p>Sudent Name : @studentDetail.StudentName</p>
- </div>

The following will be the output for the ViewData

ViewBag
It was introduced with MVC 3 and it is a dynamic type collection. It uses dynamic features of C# which is introduced with C# 4.0, so that is why there is no type checking, no intellisense, no design time error or no early binding with ViewBag. Sometimes, to use ViewBag is called Weak Type Programming.
The life to ViewBag’s data lies during the current request. After the current request, the value of ViewBag is discarded. It also does not require typecasting for getting the data as ViewData. So, in single word ViewBag is dynamic wrapper around ViewData. Dynamic features of C# are used internally.
The following are the syntax for ViewBag.
- ViewBag.name=value;
- ViewContext.ViewBag.name=value;
- ViewContext.Controller.ViewBag.name=value;
- using PassingDataInMVC.Models;
- using System.Web.Mvc;
- namespace PassingDataInMVC.Controllers
- {
- public class StudentController : Controller
- {
- // GET: Student
- public ActionResult Index()
- {
- Student model = new Student() {
- StudentId=10,
- StudentName="Mukesh Kumar",
- Address="New Delhi"
- };
- ViewBag.StudentDetail = model;
- ViewBag.Message = "This is Sudent Details using ViewBag";
- return View();
- }
- }
- }
- @{
- ViewBag.Title = "Index";
- }
- <h2>Index</h2>
- <div>
- <h4>Student</h4>
- <hr />
- <dl class="dl-horizontal">
- @{
- var studentDetail = ViewBag.StudentDetail;
- }
- </dl>
- <h2> @ViewBag.Message </h2>
- <br />
- <p>Sudent Id : @studentDetail.StudentId</p>
- <p>Sudent Name : @studentDetail.StudentName</p>
- </div>

The following will be output for ViewBag

TempData
The following are the syntax for passing data using TempData:
- TempData[“name”]=value;
- ViewContext.TempData[“name”]=value;
- ViewContext.Controller.TempData[“name”]=value;
- using PassingDataInMVC.Models;
- using System.Web.Mvc;
- namespace PassingDataInMVC.Controllers
- {
- public class StudentController: Controller
- {
- // GET: Student
- public ActionResult Index()
- {
- Student model = new Student()
- {
- StudentId = 10,
- StudentName = "Mukesh Kumar",
- Address = "New Delhi"
- };
- TempData["StudentDetail"] = model;
- TempData["Message"] = "This is Sudent Details using TempData";
- return RedirectToAction("Check");
- }
- public ActionResult Check()
- {
- var studentDetail = TempData["StudentDetail"];
- var message = TempData["Message"];
- return View();
- }
- }
- }


Note:
- If you use the following configuration into config file then you cannot use TempData.
- <sessionState mode="Off"></sessionState>
- In order to keep the data after compilation of second request, just add keep method.
- TempData.Keep(“StudentDetail”);
Thanks for reading this article, hope you enjoyed it.

IMAD AYOUBPosted May 24, 2020, 12:20 PM
Perfect. Thanks a lot for your usual professional articles. Appreciate it.
Suman VermaPosted Nov 30, 2015, 5:46 AM
nice one
Yaduveer SainiPosted Nov 30, 2015, 4:14 AM
Very nice article mukesh
Amandeep singhPosted Nov 30, 2015, 4:14 AM
nice
Aishwarya DPosted Nov 30, 2015, 1:43 AM
nice
Banketeshvar NarayanPosted Nov 30, 2015, 1:27 AM
Nice Share
Santhakumar MunuswamyPosted Nov 29, 2015, 11:44 PM
Good one
Raja TPosted Nov 29, 2015, 10:41 PM
Nice one, thanks for sharing
Mukesh KumarPosted Nov 29, 2015, 12:47 PM
Thanks Ankur and Ajay for your support
Ajay GandhiPosted Nov 29, 2015, 12:44 PM
Awesome
Ankur MistryPosted Nov 29, 2015, 5:59 AM
good one