Z 7) ViewData__ViewBag__TempData__Session
Theory :-
PASSING DATA FROM A CONTROLLER TO A VIEW IN MVC
In an ASP.NET MVC application, a controller typically performs the business logic of the application and needs to return the result to the user through a view.
You can use the following objects to pass data between controller and view:
Ø ViewData
Ø ViewBag
Ø TempData
ViewData
· Passes data from a controller to a view.
· Is a dictionary of objects that is derived from the ViewDataDictionary class.
· Some of the characteristics of ViewData are as follows:
· The life of a ViewData object exists only during the current request.
· The value of ViewData becomes null if the request is redirected.
· ViewData requires typecasting when you use complex data type to avoid error.
The general syntax of ViewData is as follows:
ViewData[“<Key>”] = <Value>;
where,
Ø Key: Is a String value to identify the object present in ViewData.
Ø Value: Is the object present in ViewData. This object may be a String, object, list, array or a different type, such as int, char, float, double DateTime etc.
Note: ViewData does not provide compile time error checking. For Example- if you mis-spell keys you wouldn’t get any compile time errors. You get to know about the error only at runtime.
VIEW BAG IN ASP.NET MVC
VIEW BAG
Ø ViewBag is also used to transfer data from a controller to a view.
Ø ViewBag exists only for the current request and becomes null if the request is redirected.
Ø ViewBag is a dynamic property based on the dynamic features introduced in C# 4.0.
Ø ViewBag does not require typecasting when you use complex data type.
The general syntax of ViewBag is as follows:
ViewBag.<PropertyName> = <Value>;
where,
Property: Is a String value that represents a property of ViewBag.
Value: Is the value of the property of ViewBag, Value may be a String, object, list, array or a different type, such as int, char, float, double DateTime etc.
Note: ViewBag does not provide compile time error checking. For Example- if you mis-spell keys you wouldn’t get any compile time errors. You get to know about the error only at runtime.
· ViewData and ViewBag can access each other data interchangeably.
TEMP DATA IN ASP.NET MVC
TEMP DATA
Ø TempData is a Dictionary object derived from the TempDataDictionary class.
Ø TempData stores data as key-value pairs. TempData[“Var”] = “Adil”
Ø TempData value must be type cast before use. Check for null values to avoid runtime error.
Ø TempData allows passing data from the current request to the subsequent request during request redirection.
The general syntax of TempData is as follows:
where,
TempData[<Key>] = <Value>;
Key: Is a String value to identify the object present in TempData.
Value: Is the object present in TempData.

· TempData in ASP.NET MVC can be used to store temporary data which can be used in the subsequent request. TempData will be cleared out after the completion of a subsequent request.
· Call TempData.Keep() to keep all the values of TempData in a third request.
SESSION IN ASP.NET MVC
SESSION
Ø Session is a property of Controller class whose type is HttpSessionStateBase.
Ø Session is also used to pass data within the ASP.NET MVC application and unlike TempData.
Ø It persist for its expiration time (default time is 20 minutes but it can be increased).
Ø Session is valid for all requests, not for a single redirect.
Ø It also requires type casting for getting data and check for null value to avoid error.
Ø Session has a performance drawback because it slow down the application that’s why it is not recommended to always use Session, Session can be used according to the situation.
Programme ::
Controller :-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace VieData_VieBagg_TempDat_Sessio.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
ViewData["var1"] = "Data Comes From ViewData"; // send data only current view
ViewBag.var2 = "Data Comes From viewBag"; // send data only Current view
TempData["var3"] = "Data Comes From TempData"; // send data Current view to SubSequent(second) view and donot send third view)
Session["var4"] = "Data Comes From Session"; // Session send data any view (20 min timeout)
string[] Students = { "Sourabh", "Hemant", "Samir", "Ketan" };
Session["var5"] = Students;
return View();
}
public ActionResult About()
{
// Access in session,tempdata,viebag,viewdata also view in About view
return View();
}
public ActionResult Contact()
{
// Access in session,tempdata,viebag,viewdata also view in Contact view
return View();
}
}
}
View :-
Index View :
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<h3>ViewData data view :- @ViewData["var1"]</h3>
<h3>ViewBag data view :- @ViewBag.var2</h3>
<h3>TempData data view :- @TempData["var3"]</h3>
<h3>Session data view :- @Session["var4"]</h3>
<h3>Access Session Data Using Array :-</h3>
<ul>
@{
foreach(string i in (string[]) Session["var5"])
{
<h4><li>@i</li></h4>
}
}
</ul>
ABOUT VIEW :
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<h3>ViewData data view :- @ViewData["var1"]</h3>
<h3>ViewBag data view :- @ViewBag.var2</h3>
<h3>TempData data view :- @TempData["var3"]</h3>
<h3>Session data view :- @Session["var4"]</h3>
<h3>Access Session Data Using Array :-</h3>
<ul>
@{
foreach(string i in (string[]) Session["var5"])
{
<h4><li>@i</li></h4>
}
}
</ul>
CONTACT VIEW :-
@{
ViewBag.Title = "Contact";
}
<h2>Contact</h2>
<h3>ViewData data view :- @ViewData["var1"]</h3>
<h3>ViewBag data view :- @ViewBag.var2</h3>
<h3>TempData data view :- @TempData["var3"]</h3>
<h3>Session data view :- @Session["var4"]</h3>
<h3>Access Session Data Using Array :-</h3>
<ul>
@{
foreach (string i in (string[])Session["var5"])
{
<h4><li>@i</li></h4>
}
}
</ul>
Comments
Post a Comment