Introduction
Before we discuss the difference between ViewData, ViewBag, TempData, and Session. Let's learn about the main use of transferring data from the Controller to View in MVC. ViewData, ViewBag, TempData, and Session are weak types, which means if you misspell a variable name it throws an error in runtime, not compile time.
Transporting data from the controller to view
A controller is responsible for handling overall communication between model and view. It will access data from the model and send to View. They are:
- ViewData
- ViewBag
- TempData
- Session
View Data
- It is a property of ControllerBase (Abstract class) class
- It is a type of ViewDataDictionary
- It is dictionary type collection with key and value pair
Syntex
ViewData.Add("Employees", "Ankit Sahu");
OR
ViewData["Employees"] = "Ankit Sahu";
- It cannot return complex values directly on a callback
- It is available for the current request.Data in ViewData and is not available across the request
- It's present in MVC framework 1.0
- ViewData is faster in compression to ViewBag
- Its value becomes NULL if redirection has occurred.
- public abstract class ControllerBase: IController
- {
- public ViewDataDictionary ViewData { get; set; }
- }
Step 1
Add DemoController in MVC application
- public class DemoController : Controller
- {
-
- public ActionResult Index()
- {
- ViewData["EmployeeName"] = "Ankit Sahu";
- ViewData["KnownTechnology"] = new List<string>() { "ASP.Net", "SQL Server", "MVC", "Angular" };
- return View();
- }
- }
Step 2
Add Index View
- @{
- ViewBag.Title = "Index";
- }
- <h2>Index</h2>
- Employee Name :- @ViewData["EmployeeName"];
- <br />
- Known Technologys :-
- <ul>
- @foreach (var technology in (List<string>)ViewData["KnownTechnology"])
- {
- <li>
- @technology
- </li>
- }
- </ul>
ViewBag
It is a property of ControllerBase (Abstract class) class
Dynamic type stores values in implicitly typed references. Hence they are resolved only in runtime.
"Ankit Sahu";
- It can return complex values directly on a callback.
- It is available for the current request. Data in ViewBag is not available across the request
- It's present in MVC framework 3.0
- ViewBag is slower in compression to ViewData
- Its value becomes NULL if redirection has occurred.
- public abstract class ControllerBase: IController
- {
- public dynamic ViewBag { get; }
- }
Step 1
Add DemoController in MVC application
- public class DemoController : Controller
- {
-
- public ActionResult Index()
- {
- ViewBag.EmployeeName = "Ankit Sahu";
- ViewBag.KnownTechnology = new List<string>() { "ASP.Net", "SQL Server", "MVC", "Angular" };
- return View();
- }
- }
Step 2
Add Index View
- @{
- ViewBag.Title = "Index";
- }
- <h2>Index</h2>
- Employee Name :- @ViewBag.EmployeeName;
- <br />
- Known Technologys :-
- <ul>
- @foreach (var technology in ViewBag.KnownTechnology)
- {
- <li>
- @technology
- </li>
- }
- </ul>
TempData
- It is a property of ControllerBase (Abstract class) class
- It is a type of TempDataDictionary
- It is a dictionary type collection with key and value pair
Syntex
TempData.Add("Employees", "Ankit Sahu");
OR
TempData["Employees"] = "Ankit Sahu";
- It cannot return complex values directly on a callback.
- It is available for the current request. Data in TempData is not available across the request.
- It is implicit and available only for all context
- It's present in MVC framework 1.0
- Call TempData.Keep() to keep all the values of TempData in a third request.
- public abstract class ControllerBase: IController
- {
- public TempDataDictionary TempData{ get; set; }
- }
Step 1
Add DemoController in MVC application
- public class DemoController : Controller
- {
-
- public ActionResult Index()
- {
- TempData["EmployeeName"] = "Ankit Sahu";
- TempData["KnownTechnology"] = new List<string>() { "ASP.Net", "SQL Server", "MVC", "Angular" };
- return View();
- }
- }
Step 2
Add Index View
- @{
- ViewBag.Title = "Index";
- }
- <h2>Index</h2>
- Employee Name :- @TempData["EmployeeName"];
- <br />
- Known Technologys :-
- <ul>
- @foreach (var technology in (List<string>)TempData["KnownTechnology"])
- {
- <li>
- @technology
- </li>
- }
- </ul>
Session
- It is a property of Controller(Abstract class) class
- It is a type of HttpSessionStateBase
- It is dictionary type collection with key and value pair
Syntex
Session .Add("Employees", "Ankit Sahu");
OR
Session ["Employees"] = "Ankit Sahu";
- It cannot return complex values directly on a callback
- It is available for the current request. Data in Session data is available across the request
- It's present from MVC framework 1.0
- All session information is stored in server side.
- public abstract class ControllerBase: IController
- {
- public HttpSessionStateBase Session { get; }
- }
Example:
Step 1
Add HomeController in MVC application
- public class HomeController : Controller
- {
- public ActionResult Index()
- {
- Session["EmployeeName"] = "Ankit Sahu";
- Session["KnownTechnology"] = new List<string>() { "ASP.Net", "SQL Server", "MVC", "Angular" };
- return View();
- }
- }
Step 2
Add View Controller
- @{
- ViewBag.Title = "Index";
- }
-
- <h2>Index</h2>
- <br>
- Employee Name :- @Session["EmployeeName"];
- <br />
- Known Technologys :-
- <ul>
- @foreach (var technology in (List<string>)Session["KnownTechnology"])
- {
- <li>
- @technology
- </li>
- }
- </ul>
-
Thank you for reading! !!!
| | | | | | | | | |
Text-to-speech function is limited to 200 characters