Thursday, November 21, 2019

Difference between viewdata viewbag tempdata and session in asp net mvc

asp.net mvc tutorials ViewData

ViewData in Asp.net MVC
  1. ViewData is similar to ViewBag and also the same functionality as ViewBag
  2. it is used to transfer the values from Controller to View.
  3. it is valid during the current request only.
  4. The Value of ViewData Must be typecast before use
  5. ViewData derived from ViewDataDictionary.
  6. The syntax to store value looks like below in Controller

           ViewData["alltechgeeks"]="All Tech Geeks";

  1.  The value displayed as in UI

             @ViewData["alltechgeeks"]

Example:

public ActionResult Index()
{
    IList<Company>  CompanyList= new List<Company>();
    CompanyList.Add(new Company(){ CompanyName = "X" });
    CompanyList.Add(new Company(){ CompanyName = "Y" });
    CompanyList.Add(new Company(){ CompanyName = "Z" });

    ViewData["companies"] = CompanyList;
  
    return View();
}


What is VIewBag?
What is TempData?
What is ViewData?

No comments: