---
title: "ViewBag in ASP.NET MVC"  
description: "ViewBag is a property of a Controller class that we generally used to share data between Controller and View and. ViewBag objects is wrapper around Vi"  
author: "Chris Anderson"  
published: 2011-10-08  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/769/viewbag-in-asp-dot-net-mvc  
category: "asp.net mvc"  
tags: ["asp.net mvc"]  
reading_time: 2 minutes  

---

# ViewBag in ASP.NET MVC

ViewBag is a property of a Controller class that we generally used to share data between Controller and View and. ViewBag objects is wrapper around ViewData that is used to create dynamic properties for ViewBag.\

The basic difference between ViewData and ViewBag is that in ViewData instead creating dynamic properties we use properties of Model to transport the Model data in View and in ViewBag we can create dynamic properties without using Model data.

##### How to use ViewBag?

Here I am explaining how to create dynamic ViewBag properties and displaying properties information in View.

In Controller class we created a several dynamic ViewBag properties i.e. Hobbies, Username, Age, and MaritalStatus that stores user information. For a hobby I use a List class from System.Collections.Generic because a user can have various hobbies.

```
using System.Web.Mvc;
using System.Collections.Generic;

namespace ViewBag.Controllers
{
     public class HomeController :  Controller
    {

         public ActionResult Index()
         {
            List<string> hobby = new List<string>();
            hobby.Add("Cricket");
            hobby.Add("Football");
            hobby.Add("Chess");

            ViewBag.Hobbies = hobby; //hobby is List
            ViewBag.UserName = "Rohit ";
            ViewBag.Age = 19;
            ViewBag.MaritalStatus = "Unmarried";
            return View();

         }
    }
}
```

In the above View we are accessing various dynamic ViewBag properties that we

\

are created in Controller class.

```
   <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>  <!DOCTYPE html>  <html><head runat="server">     <title>Index</title></head><body>     <div>            <!-- Retreiving Dynamic properties -->         <p>            My name is <b><%: ViewBag.UserName %></b>,             I am<b><%: ViewBag.Age %></b> years old.            I am <b><%: ViewBag.MaritalStatus %></b>.            <br /><br />                I like the following things:         </p>         <ul>            <% foreach (var hobby in ViewBag.Hobbies) { %>            <li>                <font color="blue"><%: hobby %></font>            </li>            <% } %>         </ul>     </div></body></html>
```

When you run your MVC application it displays the output like below:

![ViewBag in ASP.NET MVC](https://www.mindstick.com/mindstickarticle/13768801-20cf-4ff4-b3fa-f7007917b027/images/12b3bd6f-26ad-4ad5-9ae0-a817823ba840.png)

---

Original Source: https://www.mindstick.com/articles/769/viewbag-in-asp-dot-net-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
