Tuesday 2 January 2018

What is ViewBag: How It Works & Code Examples in MVC

What is ViewBag: How It Works & Code Examples in MVC
Description:

In this example we explain that how to use ViewBag in MVC.or implement functionality of the ViewBag in MVC Razor View.so here we demonstrate that what is ViewBag and use of the ViewBag in MVC Razor View.

Generally, ViewBag is the dynamic property of the C# 4.0 dynamic features. ViewBag is used for the passing value from controller to view and no need of casting data.it is only available on current request.
Controller:

public class EmployeeController : Controller
    {

        // GET: First
        public ActionResult Index()
        {
            ViewBag["Message"] = "Hello this is the Example of the ViewBag!";
            return View();
        }
    }

View: 

<html>
<head>
  
    <title>ASP.Net MVC: ViewBag Tutorial with example</title>
</head>
<body>
    <div>
        @ViewBag["Message"];
    </div>
</body>
</html>


This entry was posted in :

0 comments:

Post a Comment