Showing posts with label ViewData VS ViewBag Vs TempData in MVC. Show all posts
Showing posts with label ViewData VS ViewBag Vs TempData in MVC. Show all posts

Difference Between ViewData, ViewBag and TempData in MVC

ViewData is used to pass data from controller to view.
It is derived from ViewDataDictionary class.
It is available for the current request only.
Requires typecasting for complex data type and checks for null values to avoid error.
If redirection occurs, then its value becomes null.

In this example, a string value is set in the ViewData object in Controller and it is then displayed in View.
Controller
public class FirstController : Controller
{
    public ActionResult Index()
    {
        ViewData["Message"] = "Hello MVC!";
        return View();
    }
}

View
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    <div>
        @ViewData["Message"]
    </div>
</body>
</html>

--------------------------------------

ViewBag is also used to pass data from the controller to the respective view.
ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
It is also available for the current request only.
If redirection occurs, then its value becomes null.
Doesn’t require typecasting for complex data type.

In this example, a string value is set in the ViewBag object in Controller and it is then displayed in View.
Controller
public class FirstController : Controller
{
    public ActionResult Index()
    {
        ViewBag.Message = "Hello MVC!";
        return View();
    }
}

View
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    <div>
        @ViewBag.Message
    </div>
</body>
</html>

--------------------------------------

TempData is derived from TempDataDictionary class.
TempData is used to pass data from the current request to the next request.
It 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.
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.

In this example, a string value is set in the TempData object in Controller and it is redirected to another Controller and finally it is displayed in View.
First Controller
public class FirstController : Controller
{
    public ActionResult Index()
    {
        TempData["Message"] = "Hello MVC!";
        return new RedirectResult(@"~\Second\");
    }
}

Second Controller
public class SecondController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

View of Second Controller
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    <div>
        @TempData["Message"];
    </div>
</body>
</html>

Difference Between Boxing and Unboxing in C#

Boxing is a procedure of converting a value type to an object type. Here, the value type is stored on the stack and the object type is stored in the heap memory.

Let’s understand Boxing with an example :
int i = 24;
object ob = i; // Box the integer type n into object type ob.

In above code, the integer type i containing value 24 is stored on the stack and is copied to the object type ob. An object type is now referring to an integer value.  Now the “int i” also contain value 24 and the “object type ob” also contain value 24, but both the values are independent of each other i.e. if you change the value of i, it won’t reflect the change in the value of ob.



Unboxing is a conversion of the object type to the value type. In Unboxing the value of boxed object type stored on the heap is transferred to the value type that is stored on the stack. Unlike Boxing, the Unboxing has to be done explicitly. The object type is explicitly cast to the value type, and the value type must be same as value the object type is referring to.

Let’s understand Unboxing with an example :
int i = 24;
object ob = i; // Box the integer type n into object type ob.
int j = (int) ob; // Unbox the integer value stored in object type ob to integer type y.