Like science fiction, diverse worlds of the future?.. like gloomy cyberpunk, neo-modern utopia, then take a look at this blog -
SCI-fi arts. There you will find many new beautiful science fiction artworks from the most famous authors.
ViewData is defined as a dictionary of objects and derived from ViewDataDictionary class. We can access data value by using string as a key. It is the
type-safe and requires typecasting for the data type. It also avoids error and check for null reference at run time. It is mainly accessible only during current request.
Example
We are creating the controller and returning a view to the browser. This controller passes ViewData to the view.
Controller
1. using System;
2. using System.Collections.Generic;
3. using System.Web.Mvc;
4. namespace ViewBagExample.Controllers
5. {
6. public class ViewBagController : Controller
7. {
8. // GET: ViewBag
9. public ActionResult Index()
10. {
11. List<string> Courses = new List<string>();
12. Courses.Add("J2SE");
13. Courses.Add("J2EE");
14. Courses.Add("Spring");
15. Courses.Add("Hibernates");
16. ViewData["Courses"] = Courses;
17. return View();
18. }
19. }
20. }
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Like science fiction, diverse worlds of the future?.. like gloomy cyberpunk, neo-modern utopia, then take a look at this blog - SCI-fi arts. There you will find many new beautiful science fiction artworks from the most famous authors.
ViewData
ViewData is defined as a dictionary of objects and derived from ViewDataDictionary class. We can access data value by using string as a key. It is the type-safe and requires typecasting for the data type. It also avoids error and check for null reference at run time. It is mainly accessible only during current request.
Example
We are creating the controller and returning a view to the browser. This controller passes ViewData to the view.
Controller
View
Output:
It produces the following output to the browser.