ViewData in Asp.net MVC
- ViewData is similar to ViewBag and also the same functionality as ViewBag
- it is used to transfer the values from Controller to View.
- it is valid during the current request only.
- The Value of ViewData Must be typecast before use
- ViewData derived from ViewDataDictionary.
- 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();
}
|
---|
Post a Comment (0)