Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Part 6 - ViewData and ViewBag in mvc

Suggested Videos 
Part 3 - Creating your first asp.net mvc application
Part 4 - Controllers in an mvc application
Part 5 - Views in an mvc application



In this video we will discuss
1. What is ViewData
2. What is ViewBag
3. Difference between ViewData and ViewBag



Both ViewData and ViewBag are used to pass data from a controller to a view. ViewData is a dictionary of objects that are stored and retrieved using strings as keys. The syntax of ViewData is very similar to that of ViewState, SessionState and ApplicationState.
// Storing data in ViewData
ViewData["YourData"] = "SomeData";

// Retrieving data from ViewData
string strData = ViewData["YourData"].ToString();

ViewData does not provide compile time error checking. For example, if you mis-spell the key names you wouldn't get any compile time error. You get to know about the error only at runtime.

ViewBag uses the dynamic feature that was introduced in to C# 4.0. It allows an object to have properties dynamically added to it. Using ViewBag the above code can be rewritten as below.
// Storing data in ViewBag
ViewBag.YourData = "SomeData";

// Retrieving data from ViewBag
string strData = ViewBag.YourData;

Just like ViewData, ViewBag does not provide compile time error checking. For example, if you mis-spell the property name, you wouldn't get any compile time error. You get to know about the error only at runtime.

Internally ViewBag properties are stored as name/value pairs in the ViewData dictionary.

Please Note: To pass data from controller to a view, It's always a good practice to use strongly typed view models instead of using ViewBag & ViewData. Strongly typed view models  provide compile time error checking. We will discuss view models in a later video session.

17 comments:

  1. HIIII VENKAT...THANQ SO MUCH FOR POSTING VIDEOS ON MVC...
    IT'S REALLY AWESOME. IT'S REALLY HELPFUL FOR US

    ReplyDelete
  2. Thank You so much for providing us such a nice tutorials.thanks again and again.

    ReplyDelete
  3. Hi venkat i try to to some code but got error i am not sure why i am geting null point exception here is my code


    public ActionResult Index()
    {
    ViewBag.Message = new List() {"Ankit","Mark","Chris" };
    ViewBag.Work = new List() { ".net", "Java", "C#" };
    return View();
    }


    Name :
    @foreach (string name in ViewBag.Message)
    {@name }
    Work :@foreach (string work in ViewBag.Work)
    {@work}

    also question how can we do display list data from Viewdata.

    ReplyDelete
    Replies
    1. ViewBag.Message = new List string { "Ankit", "Mark", "Chris" };
      ViewBag.Work = new List string { ".net", "Java", "C#" };

      Delete
    2. public ActionResult Index()
      {

      ViewBag.Message = new List { "Ankit", "Mark", "Chris" };
      ViewBag.Work = new List{ ".net", "Java", "C#" };
      return View();
      }

      Delete
    3. Use the following Lines of Code:
      ViewBag.Message = new List {"Sameer","Kamran","Luqman" };
      ViewBag.Tech = new List { ".Net", "SQL", "Microsoft" };
      return View();

      Delete
    4. Thanks for the detailed presentation

      Delete
  4. Try the following ...

    ViewBag.Message = new List () { "Ankit", "Mark", "Chris" };
    ViewBag.Work = new List () { ".net", "Java", "C#" };

    ReplyDelete
  5. I hope it will work fine..
    public ActionResult Index()
    {
    ViewBag.Message=new List{"Ankit","Mark","Chris"};
    ViewBag.Work=new List{".net","Java","C#");
    return View();
    }

    ReplyDelete
  6. how send asp.net webform data to mvc controller?

    ReplyDelete
  7. public ActionResult Index()
    {
    List Countries = new List()
    {
    "Nepal",
    "Usa",
    "Australia",
    "UK"
    };
    ViewBag.Countries = Countries;
    return View();
    }
    public ActionResult Countries()
    {
    ViewData["Countries"] = new List(){
    "Nepal",
    "Russia",
    "Japan"
    };
    return View();
    }

    ReplyDelete
  8. Really Nice...

    public ActionResult Index()
    {
    ViewBag.Countries = new List()
    {
    "India",
    "US",
    "UK",
    "Canada"
    };

    ViewData["Message"] = new List { "Sameer", "Kamran", "Luqman" };
    ViewData["Tech"] = new List { ".Net", "SQL", "Microsoft" };
    return View();
    }

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. // Please try below, it should work!!!

    public ActionResult Index()
    {
    ViewBag.Message = new List() {"Ankit","Mark","Chris" };
    ViewBag.Work = new List() { ".net", "Java", "C#" };
    return View();
    }


    Name :
    @foreach (string name in ViewBag.Message)
    {@name }
    Work :@foreach (string work in ViewBag.Work)
    {@work}

    ReplyDelete
  11. Hello Sir
    You and your knowledge is awesome. I enjoy your tutorial.

    How can to display two different list data from Viewdata
    Controller Code

    public ActionResult Index()
    {
    ViewBag.Message = new List() { "Ankit", "Mark", "Chris" };
    ViewBag.Work = new List() { ".net", "Java", "C#" };
    return View();
    }
    we have to pass list type as argument for new list.
    View code :


    @foreach (string item in ViewBag.Message)
    {
    @item
    }




    @foreach (string item in ViewBag.Work)
    {
    @item
    }

    ReplyDelete
  12. Thank you Venkat sir....my heartfelt gratitude towards u

    ReplyDelete

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.