ViewBag and
ViewData is the technique used for passing data from Controller to View in MVC
architecture. And TempData used for passing data from controller to controller.
ViewData:
Main use of
ViewData is that when we have to pass data from Controller to View we use
ViewData. It Is a Dictionary of objects and it is derived from ViewDataDictionary
and its key is a string. When View is rendered on the user browser ViewData data
becomes null. In the redirection, ViewData does not maintain a state.
Let’s see
how to use ViewData:
For Assigning
Values to ViewData, we use the ViewData keyword with a dictionary.
public ActionResult SampleViewDataMethod()
{
ViewData["abc"] = "Your contact
page.";
return View();
}
And for accesing values from ViewData:
<input type="text" value="@ViewData["abc"]" />
ViewData
basically use a dictionary of the object so when we use ViewData for the complex object
we have to must Convert an object to that complex type (tpecasting). And also you
have to check null values for avoiding error.
ViewBag:
ViewBag is
also used for passing data from controller to view and it basically uses
dynamic property newly added in c# 4.0. When you use ViewBag in the case of a complex
object, no need to typecast object because of this dynamic property.
Let’s see
How to use ViewBag:
For
Assigning Values to ViewBag see below code:
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
And for
accessing values from the ViewBag
@ViewBag.Message
TempData:
Tempdata is
used for passing data from controller to controller means in the next request
only. Tempdata uses Session state in the background. If we disable session TempData
throws an exception.
TempData
uses Dictionary which is inherited from TempDataDictionary with string as a
key.
When we store
information in the TempData, it is accessible to a subsequent request.
TempData is
also in the form of a Dictionary of the object so we have to first check object is
null and when we use complex objects we have to typecast it.
Let’s see how to use TempData:
public ActionResult SampleTempDataMethod()
{
TempData["abc"] = "Your contact
page.";
return RedirectToAction("About");
}
public ActionResult About()
{
string abc = TempData["abc"].ToString();
return View();
}
If you read
the data from TempData and you want to keep Data is subsequent Request also we
can use keep() method of TempData. It keeps the data for subsequent requests
also after its one use.
public ActionResult About()
{
string abc = TempData["abc"].ToString();
TempData.Keep();
return View();
}
In the above method, all TempData objects are kept for the next request. But if you want to keep a specific object you can also use an overloaded version of the keep method like below:
public ActionResult About()
{
string abc = TempData["abc"].ToString();
TempData.Keep("abc");
return View();
}
There is another one method for TempData to access values
from TempData which is peek().
public ActionResult About()
{
string abc = TempData.Peek("abc").ToString();
return View();
}
Peek() method has one advantage it maintains data in the next subsequent request also. So we don’t need to use the keep method for this. In a single statement, we can do this.
0 Comments