asp.net-mvc ViewData, ViewBag, TempData

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Introduction

ViewData and ViewBag are used to transfer data from controller to view.

ViewData is nothing but a dictionary of objects and it is accessible by string as key.

ViewBag is very similar to ViewData. ViewBag is a dynamic property. ViewBag is just a wrapper around the ViewData.

TempData keeps data for the time of HTTP Request, which means that it holds data between two consecutive requests. TempData helps us to transfer data between controllers or between actions. Internally uses session.

Syntax

  1. ViewData[key] = value;

  2. ViewBag.Key = value;

  3. TempData[key] = value;



Got any asp.net-mvc Question?