Ravi Vishwakarma is a dedicated Software Developer with a passion for crafting efficient and innovative solutions. With a keen eye for detail and years of experience, he excels in developing robust software systems that meet client needs. His expertise spans across multiple programming languages and technologies, making him a valuable asset in any software development project.
Ravi Vishwakarma
12-Jun-2025In ASP.NET MVC,
TempData
,ViewData
, andViewBag
are used to pass data from controllers to views, but they differ in lifetime, usage, and behavior.Quick Comparison Table
ViewData
ViewBag
TempData
Dictionary<string, object>
ViewData
)TempDataDictionary
(string
keys)ViewData
, more conciseViewData["Name"]
ViewBag.Name
TempData["Name"]
Detailed Breakdown
ViewData
ViewDataDictionary
)ViewBag
ViewData
using dynamicTempData
RedirectToAction
)Session
— so session must be enabledWhen to Use What?
ViewBag
orViewData
ViewBag
orViewData
TempData
TempData
ViewModel
insteadTip: Reuse ViewBag/ViewData with Partial Views
Both
ViewBag
andViewData
are passed automatically to partial views:Pitfalls
TempData.Keep()
.