So in this article we will learn about ViewBag in short description which will understandable for beginners as wel as students .
What is ViewBag?
ViewBag is used to pass data from controller to view for single request.
- Used to pass data from controller to view.
- The value assigned to ViewBag is only available for single request, if redirection happens then value will be null.
- It is dynamic property of ControllerBase class which internally uses System.Dynamic.ExpandoObject() .
- Its uses dynamic data type to hold data which was introduced in C# 4.0.
- Since it uses dynamic data type so it’s does not require type conversion such as int to string or string to int etc .
Write the following code into your controller class
//Assigning value to viewBag ViewBag.msg="Vithal Wadje";
Accessing ViewBag value in View
<div> @ViewBag.msg </div>
Related Article .
I hope this article is useful for all readers . If you have a suggestion related to this article then please contact me.To learn ASP.NET MVC step by step please refer following articles