In web application maintaining the state of the application is very important to pass the data between the requests, ASP.NET uses lots of techniques to pass data between
requests in which most of the techniques uses the Viewstate which slows the page load time, So in this article we will learn about ASP.NET MVC ViewBag, ViewData and TempData which is used in ASP.NET MVC application to pass data between the requests which not using Viewstate, Lets learn about them in short description with example which will understandable for beginners as wel as students .
Key points
Write the following code into your controller class
In the above code ViewBag is the keyword and msg is the dynamic property . you can assign any property like msg because this is not predefined you need to assign as per your wish .
Accessing ViewBag value in View
In the above way we can access the ViewBag value into the view, I hope from all preceding example you have learned about the ViewBag.
ViewData is used to pass data from controller to view for single request which holds the data in key-value pairs were key will be string type and value is object type.
Key points
- Used to pass data from controller to view.
- The value assigned to ViewBag is only available for single request, if redirection happens then value will be null.
- It is dynamic property of ControllerBase class which internally uses System.Dynamic.ExpandoObject() .
- Its uses dynamic data type to hold data which was introduced in C# 4.0.
- Since it uses dynamic data type so it’s does not require type conversion such as int to string or string to int etc .
Write the following code into your controller class
//Assigning value to viewBag ViewBag.msg="Vithal Wadje";
Accessing ViewBag value in View
<div> @ViewBag.msg </div>
In the above way we can access the ViewBag value into the view, I hope from all preceding example you have learned about the ViewBag.
ViewData is used to pass data from controller to view for single request which holds the data in key-value pairs were key will be string type and value is object type.
Key points
There are two ways to declare or assign the value to the ViewData which are as follows
Write the following code into your controller class
Method :1
In the above code City is the key and Mumbai is the value for ViewData object.
Accessing ViewData value in View
In the above way we can access the ViewData value into the view.
Method :2
In the above code City is the key and Mumbai is the value for ViewData object.
Accessing ViewData value in View
In the above way we can access the ViewData value into the view, I hope from preceding example you have learned about ViewData
TempData is used to pass data from controller to controller and also controller to view for current request.
We can assign the value to the TempData in the following way
In the above code CityName is the key and Latur is the value for TempData object.
Accessing TempData value in View
In the above way we can access the TempData value into the view.
Accessing TempData value in Controller Action method
In the above way we can access the TempData value into the controller, I hope from preceding example you have learned about the TempData.
Summary
I hope this article is useful for all readers to learn and know about the ViewBag, ViewData and TempData . If you have an any suggestion related to this article then please contact me.
- Used to pass data from controller to view.
- ViewData is the object of ViewDataDictionary class which holds the data by key and value pair.
- In ViewData Key is the string type and value is the object type .
- We need to handle null otherwise if ViewData value is null and we tried to access value then it throws the exception.
- The value assigned to ViewData is only available for single request, if redirection happens then value will be null.
- It is property of ControllerBase class.
- It’s require type conversion such as int to string or string to int etc .
There are two ways to declare or assign the value to the ViewData which are as follows
Write the following code into your controller class
Method :1
//Assigning value to ViewData
ViewData["City"] = "Mumbai";
Accessing ViewData value in View
<div> @ViewData["City"] </div>
Method :2
//Assigning value to ViewData
ViewData.Add("Name", "Vithal Wadje");
Accessing ViewData value in View
<div> @ViewData["Name"] </div>
TempData is used to pass data from controller to controller and also controller to view for current request.
In Web application passing data from one request to another request is very important to perform the some action on data. In
traditional ASP.NET application data passed to the session is stay's
long time time in memory as per session timeout defined in the
web.config file, But sometime we need to maintain data only for current
request instead of all requests and data should be disappears in second
request , for this situation session is not fit which maintain the data
for all request so we need to use TempData to maintain data for only
current request which saves the server memory by releasing the data when
second request fires.I hope you understand when to use TempData over
session variable.
Key points- Used to pass data from controller to controller as well as controller to view.
- It holds the Data for short time means till current request.
- TempData internally uses session variable to hold the values during request.
- TempData is the object of ViewDataDictionary class.
- We need to handle null otherwise if TempData value is null and we tried to access value then it throws the exception.
- The value assigned to TempData is only available for single request, if again new request initiated then value will become null.
- It is property of ControllerBase class.
- It’s require type conversion such as int to string or string to int etc .
We can assign the value to the TempData in the following way
//Assigning value to TempData
TempData["CityName"] = "Latur";
Accessing TempData value in View
<div> @TempData["CityName"] </div>
Accessing TempData value in Controller Action method
public ActionResult CheckTempValue() { //Reading the values of TempData var TempName = TempData["EmpName"].ToString(); return View(); }
Summary
I hope this article is useful for all readers to learn and know about the ViewBag, ViewData and TempData . If you have an any suggestion related to this article then please contact me.
Don't Forget To
- Subscribe my YouTube Chanel Compile Mode
- Subscribe free email alert of compilemode.com
- Like Facebook page .
- Follow on Twitter.
- Share to your friends
Post a Comment