30Jan

ViewData and ViewBag in MVC

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.

 

 

Related

Token Based Authentication for Web API's

Securing ASP.NET Web API using Custom Token Based AuthenticationProviding a security to the Web API&...

Read More >

ASP.NET: Audit Trail Implementation using Entity Framework

When you are working with certain projects which involves the Customer records, you might need to tr...

Read More >

Asynchronous actions in MVC 5

What does it mean?Asynchronous actions allow developers to handle more concurrent requests and can b...

Read More >

ELMAH Integration in ASP.NET MVC Application

ELMAH(Error Logging Modules And Handlers)What is ELMAH?ELMAH (Error Logging Modules and Handlers) is...

Read More >

Unit testing in .NET

Unit Testing – ExplainedA process which involves writing code to verify a system at a lower an...

Read More >

THE RAZOR VIEW ENGINE IN MVC

View Engine is responsible for rendering the view into html form to the browser.ASP.NET MVC includes...

Read More >

Creating a Simple Form with Validation Using ASP.NET MVC 4 Empty Template

There are multiple Validation attributes available in MVC. Validation attributes comes from System.C...

Read More >

Brief Journey to Asp.Net MVC Framework

Introduction: ASP.NET MVC is a  framework for building web applications that uses the mode...

Read More >

Share

Post a Comment

  • Recent
  • Popular
  • Tag
Tags
Monthly Archive
Subscribe
Name

Contact Us
  • *
  • *