Sunday, April 26, 2020

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()

    ViewData["companies"] = CompanyList;

What is VIewBag?



{

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



What is TempData?

What is ViewData?

No comments: