Canarys | IT Services

Blogs

ViewData and ViewBag in MVC

Date:
Author:
Tags:
Share

ASP.NET MVC provides us two options ViewData and ViewBag for passing data from controller to view.

ViewData and ViewBag are almost similar.

Passing data with ViewBag.

The ViewBag feature allows us to define arbitrary properties on a dynamic object and access them in a view.

The dynamic object is accessed through the Controller.ViewBag property.

Let us see how the ViewBag works through a small example.

After creating a new MVC application in the HomeController Write the following code in the Index Method.

public ViewResult Index()
{
ViewBag.Message=”Hello”;
ViewBag.Date=DateTime.Now;
return View();
}

In the above code we have defined two properties Message and Date by simply assigning values to them. No such properties existed prior to this nor did we make any preparations to create them.

hme

To read the data back in the view we simply get the same properties we set in the action methods.

Write the following code in Index.cshtml.

 

@{
ViewBag.Title=”Index”;
}

Index

The day is:@ViewBag.Date.DayOfWeek

The message is: @ViewBag.Message

ind

When using ViewBag we can send multiple objects to the view. Visual studio does not provide intellisense for dynamic objects including ViewBag.

Errors won’t be revealed until the view is rendered.

opt

Passing Data with ViewData.

The ViewData feature is similar to ViewBag but is implemented using ViewDataDictionary class rather than a dynamic object.

The ViewDataDictionary class is like a regular key/value collection and is accessed through the ViewData property of the Controller class.

Let us see a small example for ViewData. After creating MVC application write the following code in Index action method.

public ViewResult Index()
{
ViewData["Message"] = "Hello";
ViewData["Date"] = DateTime.Now;
return View();
}

To read the values back in the view write the following code in Index.cshtml.

@{
ViewBag.Title = "Index";
}

Index

The day is: @(((DateTime)ViewData["Date"]).DayOfWeek)

The message is: @ViewData["Message"]

We can see that we must cast the object that we get from the ViewData.

Note: We are less fond of ViewData since ViewBag feature is available.

Similarities between ViewData and ViewBag

  1. Helps to maintain data when you move from controller to view.
  2. Used to pass data from controller to view.
  3. Short life which means value becomes null after redirection.

Differences between ViewData and ViewBag

ViewData

 

ViewData is a dictionary of objects that is derived from ViewDataDictionary class and accessible using strings as keys.

 

ViewData requires typecasting for complex data type and check for null values to avoid error.

ViewBag

 

ViewBag is a dynamic property that takes advantage of the new dynamic features

 

 

ViewBag doesn’t require typecasting for complex data type.

Leave a Reply

Your email address will not be published. Required fields are marked *

Reach Us

With Canarys,
Let’s Plan. Grow. Strive. Succeed.