Passing Data In ASP.NET MVC Application

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.

  1. ViewData
  2. ViewBag
  3. TempData

MVC diagram

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.

Web application

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.

mvc

Add a model class “Student.cs” with the following properties.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.   
  6. namespace PassingDataInMVC.Models  
  7. {  
  8.     public class Student  
  9.     {  
  10.         public int StudentId { getset; }  
  11.         public string StudentName { getset; }  
  12.         public string Address { getset; }  
  13.     }  
  14. }  
Add one controller “StudentController.cs” where we can see the live example how to use these objects to pass the data.

Controller

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.
  1. ViewData[“name”]=value;  
  2. ViewContext.ViewData[“name”]=value;  
  3. ViewContext.Controller.ViewData[“name”]=value;  
Get the data as in the following way.

<h2> my value is: @ViewData[“name”]</h2>

Controller
  1. @using PassingDataInMVC.Models  
  2. @{  
  3.     ViewBag.Title = "Index";  
  4.       
  5. }  
  6.   
  7. <h2>Index</h2>  
  8.   
  9. <div>  
  10.     <h4>Student</h4>  
  11.     <hr />  
  12.     <dl class="dl-horizontal">  
  13.        @{   
  14.            var studentDetail = ViewData["StudentDetail"as Student;  
  15.        }  
  16.          
  17.     </dl>  
  18.     <h2> @ViewData["Message"] </h2>  
  19.     <br />  
  20.     <p>Sudent Id : @studentDetail.StudentId</p>  
  21.     <p>Sudent Name : @studentDetail.StudentName</p>  
  22. </div>  
View
  1. @using PassingDataInMVC.Models  
  2. @{  
  3.     ViewBag.Title = "Index";  
  4.       
  5. }  
  6.   
  7. <h2>Index</h2>  
  8.   
  9. <div>  
  10.     <h4>Student</h4>  
  11.     <hr />  
  12.     <dl class="dl-horizontal">  
  13.        @{   
  14.            var studentDetail = ViewData["StudentDetail"as Student;  
  15.        }  
  16.          
  17.     </dl>  
  18.     <h2> @ViewData["Message"] </h2>  
  19.     <br />  
  20.     <p>Sudent Id : @studentDetail.StudentId</p>  
  21.     <p>Sudent Name : @studentDetail.StudentName</p>  
  22. </div>  
ViewData

The following will be the output for the ViewData

run

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.
  1. ViewBag.name=value;  
  2. ViewContext.ViewBag.name=value;  
  3. ViewContext.Controller.ViewBag.name=value;  
Controller
  1. using PassingDataInMVC.Models;  
  2. using System.Web.Mvc;  
  3.   
  4. namespace PassingDataInMVC.Controllers  
  5. {  
  6.     public class StudentController : Controller  
  7.     {  
  8.         // GET: Student  
  9.         public ActionResult Index()  
  10.         {  
  11.             Student model = new Student() {  
  12.                 StudentId=10,  
  13.                 StudentName="Mukesh Kumar",  
  14.                 Address="New Delhi"  
  15.             };  
  16.             ViewBag.StudentDetail = model;  
  17.             ViewBag.Message = "This is Sudent Details using ViewBag";  
  18.   
  19.             return View();  
  20.         }  
  21.     }  
  22. }  
View
  1. @{  
  2.     ViewBag.Title = "Index";  
  3.       
  4. }  
  5.   
  6. <h2>Index</h2>  
  7.   
  8. <div>  
  9.     <h4>Student</h4>  
  10.     <hr />  
  11.     <dl class="dl-horizontal">  
  12.        @{   
  13.            var studentDetail = ViewBag.StudentDetail;  
  14.        }  
  15.          
  16.     </dl>  
  17.     <h2> @ViewBag.Message </h2>  
  18.     <br />  
  19.   
  20.     <p>Sudent Id : @studentDetail.StudentId</p>  
  21.     <p>Sudent Name : @studentDetail.StudentName</p>  
  22. </div>  
Program code

The following will be output for ViewBag

index

TempData

It was introduced with MVC 1.0. It is used to transfer the data from one controller to another controller or it is also from one action to another action method. It is like one page to other page. TempData is derived from TempDataDictionary class. The life of TempData lies and only the target view is fully loaded. There is required typecasting for getting the data as ViewData. Session is used internally with TempData.

The following are the syntax for passing data using TempData:
  1. TempData[“name”]=value;  
  2. ViewContext.TempData[“name”]=value;  
  3. ViewContext.Controller.TempData[“name”]=value;  
Controller
  1. using PassingDataInMVC.Models;  
  2. using System.Web.Mvc;  
  3. namespace PassingDataInMVC.Controllers  
  4. {  
  5.     public class StudentController: Controller  
  6.     {  
  7.         // GET: Student  
  8.         public ActionResult Index()  
  9.         {  
  10.             Student model = new Student()  
  11.             {  
  12.                 StudentId = 10,  
  13.                     StudentName = "Mukesh Kumar",  
  14.                     Address = "New Delhi"  
  15.             };  
  16.             TempData["StudentDetail"] = model;  
  17.             TempData["Message"] = "This is Sudent Details using TempData";  
  18.             return RedirectToAction("Check");  
  19.         }  
  20.         public ActionResult Check()  
  21.         {  
  22.             var studentDetail = TempData["StudentDetail"];  
  23.             var message = TempData["Message"];  
  24.             return View();  
  25.         }  
  26.     }  
  27. }  
Temp data

Code

Note:

 

  1. If you use the following configuration into config file then you cannot use TempData.
    1. <sessionState mode="Off"></sessionState>  
  2. In order to keep the data after compilation of second request, just add keep method.
    1. TempData.Keep(“StudentDetail”);  

Thanks for reading this article, hope you enjoyed it.


Similar Articles