Introduction
In this article we will learn what is viewdata , viewbag and TempData. How to use ViewData in mvc. How to use ViewBag in MVC. How to use TempData in MVC. ViewData and ViewBag and TempData example in mvc. Difference between Tempdata,Viewdata and ViewBag in mvc.
ViewData
1. ViewData is used to pass data from controller to view
2. ViewData is derived from the ViewDataDictionary class and is basically a Dictionary object i.e. Keys and Values where Keys are String while Values will be objects.
3. Data is stored as Object in ViewData.
4. Data typcasting is required while retrieving the data from ViewData because of its Object type of data and also null vvalue check befor type casting otherwise it will break the application.
If redirection occurs, then its value becomes null
ViewBag
1. ViewBag is just a Wrapper of ViewData.Using pass data from controller to respective View.
2. ViewBag is a dynamic property and it makes use of the C# 4.0 dynamic features.
3. No typeCasting required while retrieving the data from Controller.
4. Available only for the current request.
Retrieve the Value in View
TempData
1. TempData is Key-Value Dictionary collection
2. TempData is a dictionary object and it is property of controllerBase class.
3. TempData is used to pass data from the current request to the next requestIt keeps the information for the time of an HTTP Request. This means only from one page to another. It helps to maintain the data when we move from one controller to another controller or from one action to another action
4. It requires typecasting for complex data type and checks for null values to avoid error. Generally, it is used to store only one time messages like the error messages and validation messages
This is my second controller to get the TempData value on Second controllers view.
Now here you can use the TempData Value on Second Controller named MySecond.
Also Read -
In previous articles we have learnt Transaction Commit and Rollback in sql server with example.What is Lock and how to achieve lock on sql table. Authorization in Asp.Net. How high quality content affects your Website. What is Blocking and Deadlock In SQL. Top 30 Asp.net interview question. What is the difference between null and undefined.
Check for null vs undefined in javascript.
In this article we will learn what is viewdata , viewbag and TempData. How to use ViewData in mvc. How to use ViewBag in MVC. How to use TempData in MVC. ViewData and ViewBag and TempData example in mvc. Difference between Tempdata,Viewdata and ViewBag in mvc.
ViewData
1. ViewData is used to pass data from controller to view
2. ViewData is derived from the ViewDataDictionary class and is basically a Dictionary object i.e. Keys and Values where Keys are String while Values will be objects.
3. Data is stored as Object in ViewData.
4. Data typcasting is required while retrieving the data from ViewData because of its Object type of data and also null vvalue check befor type casting otherwise it will break the application.
If redirection occurs, then its value becomes null
public ActionResult Index()
{
ViewData["UName"]= "GurujiPoint.com";
return View();
}
Now retrieve ViewData value in View
<html>
<head>
<meta name="viewport" content="width=device-width"/>
<title>Index</title>
</head>
<body>
<div>
@ViewData["UName"]
</div>
</body>
</html>
ViewBag
1. ViewBag is just a Wrapper of ViewData.Using pass data from controller to respective View.
2. ViewBag is a dynamic property and it makes use of the C# 4.0 dynamic features.
3. No typeCasting required while retrieving the data from Controller.
4. Available only for the current request.
public ActionResult Index()
{
ViewBag.UName = "GurujiPoint.com";
return View();
}
Retrieve the Value in View
<html>
<head>
<meta name="viewport" content="width=device-width"/>
<title>Index</title>
</head>
<body>
<div>
@ViewBag.UName
</div>
</body>
</html>
TempData
1. TempData is Key-Value Dictionary collection
2. TempData is a dictionary object and it is property of controllerBase class.
3. TempData is used to pass data from the current request to the next requestIt keeps the information for the time of an HTTP Request. This means only from one page to another. It helps to maintain the data when we move from one controller to another controller or from one action to another action
4. It requires typecasting for complex data type and checks for null values to avoid error. Generally, it is used to store only one time messages like the error messages and validation messages
public class MySecondController : Controller
{
public ActionResult
Index()
{
TempData["UName"] = "GurujiPoint.com";
return new
RedirectResult(@“~\MySecond\“);
}
}
public class MySecondController : Controller
{
public ActionResult
Index()
{
return View();
}
}
<html>
<head>
<title>Index</title>
</head>
<body>
<div>
@TempData["UName"];
</div>
</body>
</html>
Also Read -
In previous articles we have learnt Transaction Commit and Rollback in sql server with example.What is Lock and how to achieve lock on sql table. Authorization in Asp.Net. How high quality content affects your Website. What is Blocking and Deadlock In SQL. Top 30 Asp.net interview question. What is the difference between null and undefined.
Check for null vs undefined in javascript.
0 comments:
Post a Comment