CSDT IT Solution is a way proud to mention that the Job oriented IT Training is inclusive of platforms that have wider scope in the prevailing and forthcoming IT Job Market. These platforms are picked after a thorough research and analys is based upon the existing and prospective IT industry in mind. CSDT IT Solution provides Software Development Training in the platforms such as PHP & Mysql, .Net & SQL Server 2008/2012 , Software Testing, and Java/J2EE.Csdt It Solution Is Software Development Company in patna bihar(India).Csdt Centre is the first Software Development Training Company in Patna(Bihar).Csdt Centre is best software development training institute/company in Bihar.
In ASP.NET MVC there are three ways - ViewData, ViewBag and TempData to pass data from controller to view and in next request. Like WebForm, you can also use Session to persist data during a user session. Now question is that when to use ViewData, VieBag, TempData and Session. Each of them has its own importance. In this article I am trying to expose the use of these four.
ViewData
ViewData is a dictionary object that is derived from ViewDataDictionary class.
ViewData is used to pass data from controller to corresponding view.
It’s life lies only during the current request.
If redirection occurs then it’s value becomes null.
It’s required typecasting for complex data type and check for null values to avoid error.
ViewBag
ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
Basically it is a wrapper around the ViewData and also used to pass data from controller to corresponding view.
It’s life also lies only during the current request.
If redirection occurs then it’s value becomes null.
It doesn’t required typecasting for complex data type.
TempData
TempData is a dictionary object that is derived from TempDataDictionary class and stored in short lives session.
TempData is used to pass data from current request to subsequent request means incase of redirection.
It’s life is very short and lies only till the target view is fully loaded.
It’s required typecasting for complex data type and check for null values to avoid error.
It is used to store only one time messages like error messages, validation messages.
ViewData, ViewBag and TempData Current Request Example
public class HomeController : Controller
{
public ActionResult Index()
{
var emp = new Employee
{
EmpID=101,
Name = "Deepak",
Salary = 35000,
Address = "Delhi"
};
ViewData["emp"] = emp;
ViewBag.Employee = emp;
TempData["emp"] = emp;
return View(); }
}
@model MyProject.Models.EmpModel;
@{
Layout = "~/Views/Shared/ _Layout.cshtml";
ViewBag.Title = "Welcome to Home Page";
var viewDataEmployee = ViewData["emp"] as Employee; //need typcasting
var tempDataEmployeet = TempData["emp"] as Employee; //need typcasting
}
<h2>Welcome to Home Page</h2>
<div>
<a href="/Employees">
<img src='@Url.Content("\\Content\\ Images\\101.jpg")' alt="Best Employee "/>
</a>
<div>
This Year Best Employee is!
<h4>@ViewBag.emp.Name</h4>
<h3>@viewDataEmployee.Name</ h3>
<h2>@tempDataEmployee.Name</ h2>
</div>
ViewData, ViewBag and TempData Next Request Example
public ActionResult About()
{
var emp = new Employee {
EmpID=101,
Name = "Deepak",
Salary = 35000,
Address = "Delhi"
};
ViewData["emp"] = emp;
ViewBag.Employee = emp;
TempData["emp"] = emp;
//After the redirection, ViewData and ViewBag objects will be null
//Only TempData will exist after redirection
return new RedirectResult(@"~\About\");
}
@model MyProject.Models.EmpModel;
@{
Layout = "~/Views/Shared/ _Layout.cshtml";
ViewBag.Title = "About";
var tempDataEmployeet = TempData["emp"] as Employee; //need typcasting
}
TempData with Keep method
If you want to keep value in TempData object after request completion, you need to call Keep method with in the current action. There are two overloaded Keep methods to retains value after current request completion.
void Keep()
Calling this method with in the current action ensures that all the items in TempData are not removed at the end of the current request.
@model MyProject.Models.EmpModel;
@{
Layout = "~/Views/Shared/ _Layout.cshtml";
ViewBag.Title = "About";
var tempDataEmployeet = TempData["emp"] as Employee; //need typcasting
TempData.Keep(); // retains all strings values
}
void Keep(string key)
Calling this method with in the current action ensures that specific item in TempData is not removed at the end of the current request.
@model MyProject.Models.EmpModel;
@{
Layout = "~/Views/Shared/ _Layout.cshtml";
ViewBag.Title = "About";
var tempDataEmployeet = TempData["emp"] as Employee; //need typcasting
TempData.Keep("emp"); // retains only "emp" string values
}
Key point about TempData and TempData.Keep()
Items in TempData will only tagged for deletion after they have read.
Items in TempData can be untagged by calling TempData.Keep(key).
RedirectResult and RedirectToRouteResult always calls TempData.Keep() to retain items in TempData.
Summary
In this article I try to explain the difference between ViewData, ViewBag and TempData. I hope you will refer this article for your need. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.
http://csdt.co.in/
In ASP.NET MVC there are three ways - ViewData, ViewBag and TempData to pass data from controller to view and in next request. Like WebForm, you can also use Session to persist data during a user session. Now question is that when to use ViewData, VieBag, TempData and Session. Each of them has its own importance. In this article I am trying to expose the use of these four.
ViewData
ViewData is a dictionary object that is derived from ViewDataDictionary class.
ViewData is used to pass data from controller to corresponding view.
It’s life lies only during the current request.
If redirection occurs then it’s value becomes null.
It’s required typecasting for complex data type and check for null values to avoid error.
ViewBag
ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
Basically it is a wrapper around the ViewData and also used to pass data from controller to corresponding view.
It’s life also lies only during the current request.
If redirection occurs then it’s value becomes null.
It doesn’t required typecasting for complex data type.
TempData
TempData is a dictionary object that is derived from TempDataDictionary class and stored in short lives session.
TempData is used to pass data from current request to subsequent request means incase of redirection.
It’s life is very short and lies only till the target view is fully loaded.
It’s required typecasting for complex data type and check for null values to avoid error.
It is used to store only one time messages like error messages, validation messages.
ViewData, ViewBag and TempData Current Request Example
public class HomeController : Controller
{
public ActionResult Index()
{
var emp = new Employee
{
EmpID=101,
Name = "Deepak",
Salary = 35000,
Address = "Delhi"
};
ViewData["emp"] = emp;
ViewBag.Employee = emp;
TempData["emp"] = emp;
return View(); }
}
@model MyProject.Models.EmpModel;
@{
Layout = "~/Views/Shared/
ViewBag.Title = "Welcome to Home Page";
var viewDataEmployee = ViewData["emp"] as Employee; //need typcasting
var tempDataEmployeet = TempData["emp"] as Employee; //need typcasting
}
<h2>Welcome to Home Page</h2>
<div>
<a href="/Employees">
<img src='@Url.Content("\\Content\\
</a>
<div>
This Year Best Employee is!
<h4>@ViewBag.emp.Name</h4>
<h3>@viewDataEmployee.Name</
<h2>@tempDataEmployee.Name</
</div>
ViewData, ViewBag and TempData Next Request Example
public ActionResult About()
{
var emp = new Employee {
EmpID=101,
Name = "Deepak",
Salary = 35000,
Address = "Delhi"
};
ViewData["emp"] = emp;
ViewBag.Employee = emp;
TempData["emp"] = emp;
//After the redirection, ViewData and ViewBag objects will be null
//Only TempData will exist after redirection
return new RedirectResult(@"~\About\");
}
@model MyProject.Models.EmpModel;
@{
Layout = "~/Views/Shared/
ViewBag.Title = "About";
var tempDataEmployeet = TempData["emp"] as Employee; //need typcasting
}
TempData with Keep method
If you want to keep value in TempData object after request completion, you need to call Keep method with in the current action. There are two overloaded Keep methods to retains value after current request completion.
void Keep()
Calling this method with in the current action ensures that all the items in TempData are not removed at the end of the current request.
@model MyProject.Models.EmpModel;
@{
Layout = "~/Views/Shared/
ViewBag.Title = "About";
var tempDataEmployeet = TempData["emp"] as Employee; //need typcasting
TempData.Keep(); // retains all strings values
}
void Keep(string key)
Calling this method with in the current action ensures that specific item in TempData is not removed at the end of the current request.
@model MyProject.Models.EmpModel;
@{
Layout = "~/Views/Shared/
ViewBag.Title = "About";
var tempDataEmployeet = TempData["emp"] as Employee; //need typcasting
TempData.Keep("emp"); // retains only "emp" string values
}
Key point about TempData and TempData.Keep()
Items in TempData will only tagged for deletion after they have read.
Items in TempData can be untagged by calling TempData.Keep(key).
RedirectResult and RedirectToRouteResult always calls TempData.Keep() to retain items in TempData.
Summary
In this article I try to explain the difference between ViewData, ViewBag and TempData. I hope you will refer this article for your need. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.
http://csdt.co.in/



