ITORIAN    

  public string Welcome() {

      return "Abhimanyu's Thoughts";

  }

My First E-Book
Quick Report
Yearly Posts:
   i) Oct-Nov 2010: 12
   ii) In 2011: 380
   iii) In 2012: 32

You can find my articles and blogs on:
   i) itorian.com
   ii) c-sharpcorner.com
   iii) dotnetfunda.com
   iv) codeproject.com

Greatest Hits
People I Follow
Disclaimer
This is my personal website and the opinions I have expressed here is my own. For any accuracy I recommend to visit official websites like MSDN for Microsoft. I developed this website to share my technical skills.

Comparison between ViewBag and ViewData in MVC
In this quick post you will take a look at some key differences between ViewBag and ViewData in MVC.
Abhimanyu Kumar Vatsa

IT Faculty || I blog on Microsoft Technologies || Mindcracker MVP || Founder of ITORIAN.COM
   

Introduction

In my recent free E-Book titled “Razor View Engine in MVC 3”, written some lines on “ViewBag and ViewData”, a reader named “Krish” asked me a question as follows:

Krish: Nice one book to learn MVC and Razor, thanks for it. But the last statement you written "There is no any significant advantage in using ViewBag over ViewData, perhaps a few fewer key strokes, but nothing more." But ViewBag is using the concept of C # 4.0 i.e. dynamic creations of properties which can replace the strongly typed view.

What you think about this. Thanks I got the book.

And what I replied to him is

Author: Nice point to discuss on, it’s a most frequent question in online forums. Look at the difference "ViewData is a dictionary object that you put data into, which then becomes available to the view. ViewData is a derivative of the ViewDataDictionary class, so you can access by the familiar "key/value" syntax. The ViewBag object is a wrapper around the ViewData object that allows you to create dynamic properties for the ViewBag". I hope your point is clear now. Please reply the same.

After above discussion I decided to post some real examples on this point which I missed to put in that free e-book. So, let’s take a look at the discussion.

As we know there no any technical differences between both terms that which one to choose while developing our MVC application however there is some critical differences that we should remember.

First One

ViewBag only works when the key which is being accessed is valid identifier.

For example, if you place a value in

ViewData[“Identified having space”],

then we can’t access that value using ViewBag.

Second One

Remember, in case your parameter is dynamic then your program will fail if you are using ViewBag. For example,

@Html.TextBox(“mobile”, ViewBag.Mobile)

Above code will always fail, to fix this you need to use either ViewData[“Mobile”] or use casing in ViewBag like (string)ViewBag.Mobile.

Thanks for reading this post.

Happy Coding !!



Join us on Facebook. Join us on Twitter.

   



Approved Comments

Diandra 1/20/2012 8:10:56 PM
Diandra: Great insights. Relieved I'm on the same side as you.
Author: :)