Understanding ViewData And ViewBag In MVC

Overview

In an earlier part we saw about how views work in MVC . To find out do refer to this link. Here in this article we will see what ViewData and ViewBag in MVC are and how they work and what they are used for . So let's start .

Introduction

We will be using previous solutions to see the demo in this application. Let's see what ViewBag and ViewData in MVC  are.

ViewBag and ViewData are nothing but passing data from Controller to views .

views

ViewData are nothing but dictionary objects which are used to store or retrieve using string as keys .

Explanation

ViewData[“Details”]=”Values”;

ViewBag allows an object to have properties dynamically added to it .

Explanation

ViewBag.Details=”Values”;

  • Open our previous solution,

    code

As you can see we had used ViewBag object to store details like name, address and so on  -- we had used a dynamic property of viewbag called details and we had used List<string> to pass the values .

Similarly in our View section i.e Index.cshtml we had used,

code

We had passed ViewBag.Details in View and we are storing in StrDetails to show our Output . So basically what we are doing is using dynamic property in ViewBag called Details and passing data from controller to view .

NOTE:

ViewBag or View Data do not provide any compile time error. If you rename the property name and that property name is not assigned to view you will not get a compile time error,  in fact the error will show at runtime.

Let's see in ViewBag First .

We will be renaming the ViewBag.Details to ViewBag.DetailsList,

code
And kept the same thing as it is in Index.cshtml i.e in our View,

code

Now let's run the application and we will see what output we are getting.

We got this error:

code

error

It says we are passing some null value, now remember we had renamed the property as DetailsList and we haven’t assigned the same property in View but we didn’t get any error at compile time.

Now build the solution  -- still no error; when we run the solution again we get the same error .

build

Why?? Because the DetailsList property we had assigned to our controller in not passed to our View.

  • Now let's see how to use ViewData to switch back to our controller and modify this code,


Modify the Code in our View also,

code
We got an error; now hover the mouse and see what return type string is,

code

Just see the return type is string as in ViewData you can save anything --  session State, application State, and so on,

Now we will modify our cost and write it as,

code
Build and Run our Solution.

We got an output,

output
Now suppose if I rename the view from Details to DetailsList and run the application we will get an error,

code
We got the same error again,

error

To pass data from view to controller it's always good to pass data from strongly typed view data. We will see in later sections of our MVC articles what strongly typed views are. 

Conclusion

This was about ViewBag and ViewData. I hope this article was helpful.


Similar Articles