---
title: "When to use ViewBag in asp.net MVC?"  
description: "When to use ViewBag in asp.net MVC?"  
author: "Anonymous User"  
published: 2020-01-28  
updated: 2020-01-28  
canonical: https://www.mindstick.com/forum/155749/when-to-use-viewbag-in-asp-dot-net-mvc  
category: "asp.net mvc"  
tags: ["asp.net mvc"]  
reading_time: 1 minute  

---

# When to use ViewBag in asp.net MVC?

Tell me when we will use ViewBag in [asp.net MVC](https://www.mindstick.com/forum/155798/what-is-caching-in-asp-dot-net-mvc) with the help of suitable example.

## Replies

### Reply by Nishi Tiwari

ViewBag is used to allow us to share values dynamically. There are several variables which are not known by us before the program is run, and these values are entered only during the runtime. And this is where **[ViewBag shines](https://www.mindstick.com/forum/155739/define-viewbag-in-mvc)**, because we can put just about anything which we want in ViewBag.

We can use ViewBag objects for small amounts of data which are transferring it from the controller to the view. It will work well for all such cases as:

• Shopping carts

• Dropdown lists options

• Widgets

• Aggregated data

It is a great way to access data which we use but may reside outside the data model. It is also easy to use because it is implemented as a property of both controllers and view.

There is also one place when ViewBag is mandatory, and that is when we are specifying a page title on any given view. For example:

```
@{
     ViewBag.PageTitle = "Page title will be displayed on the browser tab";
}
```


---

Original Source: https://www.mindstick.com/forum/155749/when-to-use-viewbag-in-asp-dot-net-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
