ASP.NET MVC > Views - Razor Engine

Getting ViewBag / ViewData in ASP.NET MVC

How to write ViewBag / ViewData value on the razor view?

ViewBag or ViewData (“in short, ViewBag and Viewdata are temporary placeholders to store some value valid for that request only) is nothing but an object or variable whose value is generally set in the Controller action method (however it can also be set at other places like View). To write their values, simply call them as if we call any other variable. 

To store data into them, we can use below syntax 

   ViewBag.Title = "Index";   
  ViewData["MyName"] = "Sheo Narayan"; 

To write their values, call them as if you are calling any variables in Razor syntax.

<h2>ViewBag | ViewData</h2> 
@ViewBag.Title <p> @ViewData["MyName"] </p>
 Views: 15868 | Post Order: 22

Write for us

Hosting Recommendations