In the above screenshot, we have created a ViewBag with key Car inside Index action method. We will access this ViewBag when view will render.

View :

On View we have created a h2 element. We have accessed the ViewBag inside script element using @ sign used inside single quotes. Then the ViewBag value is assigned to h2 element using html method in jQuery.

UI :

The ViewBag text is fetched and assign to h2 element. The above screenshot shows how ViewBag value looks in debugging mode.