A Brief Analysis of the Differences between ViewData ViewBag and TempData in.NET MVC

  • 2021-06-28 09:06:49
  • OfStack

This paper analyses the differences among ViewData, ViewBag and TempData in.NET MVC.Share it for your reference.Specific analysis is as follows:

ViewData and TempData are dictionary types and assignments are made in a dictionary manner, such as:

ViewData["myName"]

ViewBag is a dynamic type and can be used directly by adding attribute assignments, such as:
ViewBag.myName

ViewBag and ViewData are valid only in the current Action, equivalent to View
TempData can continue to be used by turning around because its value is stored in Session.However, TempData can only be passed once and then automatically cleared by the system

The values in ViewData and ViewBag are accessible to each other because ViewData is included in the implementation of ViewBag.

I hope that the description in this paper will be helpful to your.net program design.


Related articles: