ViewBag ViewData and TempData in ASP.NET MVC

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 .

What is ViewBag?

ViewBag is used to pass data from controller to view for single request.


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 .
Example
Write the following code into your controller class
//Assigning value to viewBag
ViewBag.msg="Vithal Wadje";
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
<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.
    What is ViewData?

    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. 
    • 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 .
    Example
    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";
    In the above code City is the key and Mumbai is the value for ViewData object.
    Accessing ViewData value in View
    <div>
    @ViewData["City"]
    </div>
    In the above way we can access the ViewData value into the view.
    Method :2
    //Assigning value to ViewData
      ViewData.Add("Name", "Vithal Wadje");
    In the above code City is the key and Mumbai is the value for ViewData object.
    Accessing ViewData value in View
    <div>
    @ViewData["Name"]
    </div>
    In the above way we can access the ViewData value into the view, I hope from preceding example you have learned about ViewData
    What is TempData ? 

    TempData is used to pass data from controller to controller and also controller to view for current request.
    TempData in ASP.NET MVC by compilemode.com


    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 . 
    Example
    We can assign the value to the TempData in the following way
    //Assigning value to TempData
     TempData["CityName"] = "Latur";
    In the above code CityName is the key and Latur is the value for TempData object.
    Accessing TempData value in View
    <div>
    @TempData["CityName"]
    </div>
    In the above way we can access the TempData value into the view.
    Accessing TempData value in Controller Action method
        public ActionResult CheckTempValue()
            {
               //Reading the values of TempData
                var TempName = TempData["EmpName"].ToString();
                return View();
            }
    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.

    Don't Forget To 

    Post a Comment

    www.CodeNirvana.in

    Protected by Copyscape
    Copyright © Compilemode