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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With