In this article I will explain with an example, how to pass (send) data from Controller to View using ViewBag in ASP.Net MVC Razor.
This article will illustrate how to create a ViewBag object, store some data and then access the ViewBag object inside the View using Razor syntax in ASP.Net MVC Razor.
 
 
Pass (Send) data from Controller to View using ViewBag
In the below example, a string value is set in the ViewBag object in Controller and it is then displayed in View.
Controller
public class FirstController : Controller
{
    // GET: First
    public ActionResult Index()
    {
        ViewBag.Message = "Hello MVC!";
        return View();
    }
}
 
View
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    <div>
        @ViewBag.Message
    </div>
</body>
</html>
 
 
Screenshot
Pass (Send) data from Controller to View using ViewBag in ASP.Net MVC
 
 
Downloads