In ASP .NET MVC, ViewData, View Bag, TempData is used to maintain the state in our page/view. Viewdata, ViewBag is used to transfer date/information from controller to view in the current request. TempData use to transfer data from controller to another controller. It has capable of maintaining the data to not only on current request, it can maintain the data in next HTTP requests.
Flow Diagram :
ViewData is a dictionary object, which used to maintain data from controller to view. It is derived from viewDataDictionary class. It requires typecasting for complex data and need to check null values to avoid error as shown below.
Note: The data inside the viewdata will be available in the current request only so the value become NULL if redirection occurs.
public ActionResult Index()
var dotnetproduct = new dotnetproduct(name: “MVC”);