Posts

Showing posts with the label Difference between ViewData ViewBag and Tempdata with Example

ASP.Net MVC: ViewData, ViewBag and TempData with examples

In this section, I will explain the purpose of ViewData, ViewBag, and TempData with examples. ViewData 1. ViewData is derived from the ViewDataDictionary class and is basically a Dictionary object i.e. Keys and Values where Keys are String while Values will be objects. 2. Data is stored as Object in ViewData. 3. While retrieving, the data it needs to be Type Cast to its original type as the data is stored as objects and it also requires NULL checks while retrieving. 4. ViewData is used for passing value from Controller to View. 5. ViewData is available only for Current Request. It will be destroyed on redirection. Syntax:- C# Code: public class Parent1Controller : Controller { public ActionResult Index() { ViewData[ "Message" ] = "Welcome to Dotnet Builders Tutorials" ; return View(); } } .CSHTML CODE: < html > < head >     < meta name ="viewport" co...