Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is ViewBag.Title in Razor?

What is ViewBag.Title in ASP.NET MVC 4?

I have that View file:

@model IEnumerable<MvcMusicStore.Models.Genre>  @{     ViewBag.Title = "Store"; }  <h2>Index</h2> 

and I do not know what changing ViewBag.Title may accomplish.

like image 894
Yoda Avatar asked Jun 22 '14 17:06

Yoda


People also ask

What is ViewBag title?

ViewBag is a dynamic object, which means you can put whatever you want in to it; the ViewBag object has no defined properties until you put something inside it. The ViewBag.Title property is simply a string object. In this case it's being used in the view to actually define the Title property.

What is ViewBag in Cshtml?

ASP.NET MVC - ViewBag. The ViewBag in ASP.NET MVC is used to transfer temporary data (which is not included in the model) from the controller to the view. Internally, it is a dynamic type property of the ControllerBase class which is the base class of the Controller class.

What is ViewBag and ViewData in MVC?

ViewData and ViewBag are used for the same purpose -- to transfer data from controller to view. ViewData is nothing but a dictionary of objects and it is accessible by string as key. ViewData is a property of controller that exposes an instance of the ViewDataDictionary class. ViewBag is very similar to ViewData.


1 Answers

From the ASP.NET site:

ViewBag is a dynamic object, which means you can put whatever you want in to it; the ViewBag object has no defined properties until you put something inside it.

The ViewBag.Title property is simply a string object. In this case it's being used in the view to actually define the Title property. If you were to look in your _Layout.cshtml file you would likely see something like:

<title>@ViewBag.Title</title> 

Remember when the property was defined in the view? When the page is finally rendered, that property ends up in the HTML markup looking like:

<title>Store</title> 

Which sets the browser title.

like image 77
Justin Helgerson Avatar answered Oct 07 '22 19:10

Justin Helgerson