Add the breakpoint on ShowData() and to check the flow of data press F11.
Let see the output of preceding code.
ViewBag
A Viewbag is a collection of viewdata. A Viewbag is a dynamic property that benefits from a new feature of C# 4.0. Viewbag doesn't require typecasting for complex datatype. Viewbag is a property of the controller class. Its life exists only during the current request. If redirection occurs then its value become null.
Let us see an example.
- public class HomeController : Controller
- {
-
- public ActionResult ShowData()
- {
- ViewBag.datetime = DateTime.Now;
- ViewBag.name = "hello ia m view bag";
- return view();
- }
- }
The following is the View page code:
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>ShowData</title>
- </head>
- <body>
- <div>
- @ViewBag.datetime<br/>
- @ViewBag.name
- </div>
- </body>
- </html>
The following is the output of the preceding code:
Temp Data
Tempdata is a dictionary object
derived from the
TempDataDictionary class and is stored in a short live session. TempData is a property of the Controller class. It's life is very short and exists only until the target view is fully loaded. It's required typecasting for getting the data and to check for null values to avoid errors
. Temdata cannot pass the data from the Controller to the View. TempData persists the data ActionResult to ActionResult.
Let us see an example.
- public class HomeController : Controller
- {
-
- public ActionResult ShowData()
- {
- TempData["datetime"] = DateTime.Now;
- TempData["name"]="hello i am temp data";
- return RedirectToAction("ShowData1");
- }
-
-
- public ActionResult ShowData1()
- {
- string str = TempData["datetime"].ToString();
- string str1 = TempData["name"].ToString();
- return View("ShowData");
- }
- }
Add the breakpoint on ShowData() and ShowData1() to check the flow of data then press F11.
Select
str in the
showdata method and right-click on
str => click
Quickwatch.
Session
Session is an object derived from the
HttpSessionState class. Session is the property of the HttpContext class. Session can also pass the data from the controller to the view and Action to Action. Session is valid for all requests, not for a single redirect and it is also need typecasting to getting data and check for null values to avoid error.
Let us see an example.
- public class HomeController : Controller
- {
-
- public ActionResult ShowData()
- {
-
- Session["datetime"] = DateTime.Now;
- Session["name"] = "hello i am session";
- return RedirectToAction("ShowData1");
-
- }
-
-
- public ActionResult ShowData1()
- {
- string str = Session["datetime"].ToString();
- string str1 = Session["name"].ToString();
- Session["datetime"] = DateTime.Now;
- Session["name"] = "hello i am session";
- return View("ShowData");
-
- }
- }
The following is the View page code:
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>ShowData</title>
- </head>
- <body>
- <div>
- @Session["datetime"]<br />
-
- </div>
- </body>
- </html>
Add the breakpoint on ShowData() and ShowData1() to check the flow of data then press F11.
Let us see in the QuickWatch.
In the preceding figure we saw the role of session. session passes the data from Controller to View and Action to Action.
The following is the output of the preceding code:
Summary
This article showed how to pass data from the Controller to the View and Action to Action using
ViewBag, ViewData, TempData and
Session.