In ASP.NET MVC there are lots of techniques to pass data between requests for temporary purposes unlike session variable which will hold the value throughout the application and consume the server resources unnecessarily were we don’t need.
 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.
Key points
  • 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";
In the above code ViewBag is the keyword and msg is the dynamic property . you can assign any property like msg because this is not predefined you need to assign as per your wish .
Accessing ViewBag value in View
In the above way we can access the ViewBag value into the view.
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

Post a Comment

Protected by Copyscape
Copyright © Compilemode