Differences between viewdata, viewbag and tempdata in mvc3

Differences between viewdata, viewbag and tempdata in mvc3

 

In Asp.net MVC, we have three objects – ViewData, ViewBag and TempData to pass data from controller to view and in next request.

ViewData

1.ViewData is a dictionary object that is derived from ViewDataDictionary class.

2.ViewData is used to pass data from controller to corresponding view.

3.It’s life lies only during the current request.

4.If redirection occurs then it’s value becomes null.

5.It’s required typecasting for complex data type and check for null values to avoid error.

ViewBag

1.ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.

2.Basically it is a wrapper around the ViewData and also used to pass data from controller to      corresponding view.

3.It’s life also lies only during the current request.

4.If redirection occurs then it’s value becomes null.

5.It doesn’t required typecasting for complex data type.

TempData

1.TempData is a dictionary object that is derived from TempDataDictionary class and stored in short lives session.

2.TempData is used to pass data from current request to subsequent request means incase of redirection.

3.It’s life is very short and lies only till the target view is fully loaded.

4.It’s required typecasting for complex data type and check for null values to avoid error.

5.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 cust= new Customer
{
CustID=101,
Name = "Rajesh",

};
ViewData["cust"] = cust;
ViewBag.Customer = cust;
TempData["cust"] = cust;
return View();
}
}

return View(); }
}
@model MvcSample.Models.CustomerModel;
@{
Layout = "~/Views/Shared/_Layout.cshtml";
ViewBag.Title = "Welcome to MVC Tutorails";
var viewDataCustomer = ViewData["cust"] as Customer; //need typcasting
var tempDataCustomer = TempData["cust"] as Customer; //need typcasting
}
<h2>Welcome to MVC Tutorials</h2>
This Best behavioral customer is!
<h4>@ViewBag.cust.Name</h4>
<h3>@viewDataCustomer.Name</h3>
<h2>@tempDataCustomer.Name</h2>
</div>

ViewData, ViewBag and TempData Next Request Example

public ActionResult About()
{
var cust= new Customer {
CustID=007,
Name = "Rajesh",
};
ViewData["cust"] = cust;
ViewBag.Customer = cust;
TempData["cust"] = cust;
//After the redirection, ViewData and ViewBag objects will be null
//Only TempData will exist after redirection
return View();
}
@model MvcSample.Models.Customermodel;
@{
Layout = "~/Views/Shared/_Layout.cshtml";
ViewBag.Title = "Detail";
var tempDataCustomer = TempData["cust"] as Customer; //need typcasting
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s