public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Name = "Manav Pandya";
ViewBag.Description = "This is the Demo of ViewBag in ASP.NET MVC";
return View();
}
}
- Now, right click on Index action -> Add
View -> Leave all configuration -> click “Add”.
- Now, add below line of code into Index.cshtml
view to access ViewBag property in the view.
Code :
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<h1>@ViewBag.Name</h1>
<div>@ViewBag.Description</div>
Now, Run the project and
browse the Index action of HomeController. You will get output as shown in
below screenshot.
Final Output :
Thanks for Reading ....
See my other article also :
ASP.NET , C# , Javascript , Bootstrap , AngularJs , Model View Controller , Java , Visual Studio , Ready made projects , MVC 4.0 , .NET Core