Categories
Uncategorized

How to pass values from Controller to View using ViewBag in MVC 3

Hello, In this small article I have shown how to pass a value from a Controller to a View using ViewBag.

Below is the code.

Controller code

public class HomeController : Controller  
{
//
// GET: /Home/
public ActionResult Index()
{
ViewBag.Greet = DateTime.Now.Hour < 12 ? "Good Morning" : "Good Afternoon";
return View();
}
}

Razor code

@{  
Layout = null;
}
<!DOCTYPE html>
<html>  
<head>
<title>Index</title>
</head>
<body>
<div>
@ViewBag.Greet, World !
</div>
</body>
</html>
Output


Thanks !