What is ViewData in Asp.Net MVC

                                        
ViewData in Asp.net MVC



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?

Previous Post Next Post