Roadmap › ASP.NET MVC 5 (.NET Framework) — Folder Structure, Request Life Cycle and Features › Module: Controllers and Views
Lessons in this course (10)
5. Passing data — ViewData, ViewBag, TempData, Session and strongly typed models
Intermediate★ Must knowLesson 5 of 10
| Mechanism | Type | Lifetime | Typical use |
|---|---|---|---|
| Strongly typed model | View(model) + @model | One request | The main data — IntelliSense, compile-time checks (preferred) |
| ViewData | ViewDataDictionary (string keys) | Current request, controller → view | Small extras |
| ViewBag | dynamic wrapper over ViewData | Current request | Title, small extras (no compile-time checking) |
| TempData | Dictionary backed by Session (default) | Until read in a subsequent request | Messages after redirect (Post-Redirect-Get) |
| Session | Session["key"] | User session | Cart, wizard state |
TempData.Keep("key") keeps a value for another request; TempData.Peek("key") reads without marking it for deletion.