Table of contents
1.
Introduction
2.
ASP.NET MVC ViewBag
3.
ASP.NET MVC ViewData
4.
ASP.NET MVC TempData
5.
Frequently Asked Questions
6.
Key Takeaways
Last Updated: Mar 27, 2024

ASP.NET MVC ViewBag, ViewData, And TempData

Author Rajat Agrawal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The MVC is an application development pattern or design pattern which separates an application into three main components:-

1) Model: A Model is a part of an application that implements the logic for the application's data domain.

2) View: View is a component used to form the application's user interface. It is used to create web pages for the application.

3) Controller: Controller is the component that controls the user interaction. It works along with the model and selects the view to render the web page.

ASP.NET MVC provides three essential variables to store and pass values from controller to view. The ViewData and ViewBag are similar except TempData, with some additional features.

Let's learn about these variables one by one.

ASP.NET MVC ViewBag

MVC ViewBag is a dynamic property introduced in .Net Framework version 4.0. it is used to send data from the controller to the view page. ViewBag can get and set values dynamically; that's why it is called dynamic property. It does not require type conversion and converts type dynamically.

Example: 

Let's implement the ViewBag property to understand it properly. The controller and an Index file are given below.

Controller

using System;  
using System.Collections.Generic;  
using System.Web.Mvc;  
namespace ViewBagExample.Controllers  
{  
    public class ViewBagController : Controller  
    {  
        // GET: ViewBag  
        public ActionResult Index()  
        {  
            List<string> Courses = new List<string>();  
            Courses.Add("Computer Networks");  
            Courses.Add("Operating System");  
            Courses.Add("DBMS");  
            Courses.Add("Compiler Design");  
            ViewBag.Courses = Courses;  
            return View();  
        }  
    }  
}  
You can also try this code with Online Java Compiler
Run Code

 

View

<!DOCTYPE html>  
<html>  
<head>  
    <meta name="viewport" content="width=device-width" />  
    <title>Index</title>  
</head>  
<body>  
    <h2>List of Courses</h2>  
    <ul>  
        @{  
            foreach (var Courses in ViewBag.Courses)  
            {  
                <li> @Courses</li>  
            }  
        }  
    </ul>  
</body>  
</html>  

ASP.NET MVC ViewData

MVC ViewData is a dictionary of objects derived from ViewDataDictionary class. We can access the value by using string as a key. It is type-safe and requires typecasting for the data type. It avoids errors and checks for null reference at run time. It is accessible only during the current request.

Example:

Let's implement the ViewData property to understand it properly. 

We will be creating a controller and returning a view to the browser. This controller passes Courses ViewData to the view.

Controller

using System;  
using System.Collections.Generic;  
using System.Web.Mvc;  
namespace ViewDataExample.Controllers  
{  
    public class ViewDataController : Controller  
    {  
        // GET: ViewData  
        public ActionResult Index()  
        {  
            List<string> Courses = new List<string>();  
            Courses.Add("Computer Networks");  
            Courses.Add("Operating System");  
            Courses.Add("DBMS");  
            Courses.Add("Compiler Design");  
            ViewData["Courses"] = Courses;  
            return View();  
        }  
    }  
}  
You can also try this code with Online Java Compiler
Run Code

 

View

<!DOCTYPE html>  
<html>  
<head>  
    <meta name="viewport" content="width=device-width" />  
    <title>Index</title>  
</head>  
<body>  
    <h2>List of Courses</h2>  
    <ul>  
        @{  
            foreach (var Courses in ViewData["Courses"] as List<string>)  
            {  
                <li> @Courses</li>  
            }  
        }  
    </ul>  
</body>  
</html>  

ASP.NET MVC TempData

MVC TempData represents a set of data that persists only from one request to the next. It is derived from TempDataDictionary, and we can use its object to pass data as we did in ViewData. The value of TempData persists only from one request to the next. Retention is used to mark key to stay data for the subsequent request.

Example:

Let's implement the ViewData property to understand it properly.

Controller

using System;  
using System.Collections.Generic;  
using System.Web.Mvc;  
namespace TempDataExample.Controllers  
{  
    public class TempDataController : Controller  
    {  
        // GET: TempData  
        public ActionResult Index()  
        {  
            List<string> Courses = new List<string>();  
            Courses.Add("Computer Networks");  
            Courses.Add("Operating System");  
            Courses.Add("DBMS");  
            Courses.Add("Compiler Design");  
            TempData["Courses"] = Courses;  
            return View();  
        }  
    }  
}  
You can also try this code with Online Java Compiler
Run Code

 

View

<!DOCTYPE html>  
<html>  
<head>  
    <meta name="viewport" content="width=device-width" />  
    <title>Index</title>  
</head>  
<body>  
    <h2>List of Courses</h2>  
    <ul>  
        @{  
            foreach (var Courses in TempData["Courses"] as List<string>)  
            {  
                <li> @Courses</li>  
            }  
        }  
    </ul>  
</body>  
</html>  

Frequently Asked Questions

1.) What does MVC stand for?

Ans. MVC stands for Model-View-Controller.

2.) What is the purpose of Model in MVC?

Ans. A Model is a part of an application that implements the logic for the application's data domain.

3.) What is the purpose of View in MVC?

Ans. The View is a component used to form the application's user interface. It is used to create web pages for the application.

4.) What is the purpose of the Controller in MVC?

Ans. A controller is a component that controls user interaction. It works along with the model and selects the view to render the web page.

Key Takeaways

In this blog, we have learned about ASP.NET MVC ViewBag, ViewData, and TempData variables, their features, and usage with the help of code examples.

Apart from this, you can also expand your knowledge by referring to these articles on Features Of ASP Net and ASP Full Form. If you want to learn advanced web development blogs, you can visit Coding Ninjas Web Blogs.

Recommended Readings:

Happy Learning!!

Live masterclass